ekasetiawans / flutter_bluetooth_printer_plugin

MIT License
21 stars 36 forks source link

The app is crashed when the Bluetooth state is change. #23

Open mahmoud-haj-ali opened 1 year ago

mahmoud-haj-ali commented 1 year ago

When I'm trying to enable bluetooth, the app is crashed, and throw this exception:

E/AndroidRuntime(21981): FATAL EXCEPTION: main
E/AndroidRuntime(21981): Process: com.paymentcollector, PID: 21981
E/AndroidRuntime(21981): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.adapter.action.STATE_CHANGED flg=0x15000010 (has extras) } in id.flutter.plugins.FlutterBluetoothPrinterPlugin$1@940ada8
E/AndroidRuntime(21981):    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1796)
E/AndroidRuntime(21981):    at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
E/AndroidRuntime(21981):    at android.os.Handler.handleCallback(Handler.java:900)
E/AndroidRuntime(21981):    at android.os.Handler.dispatchMessage(Handler.java:103)
E/AndroidRuntime(21981):    at android.os.Looper.loop(Looper.java:219)
E/AndroidRuntime(21981):    at android.app.ActivityThread.main(ActivityThread.java:8668)
E/AndroidRuntime(21981):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(21981):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
E/AndroidRuntime(21981):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
E/AndroidRuntime(21981): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.app.Activity.checkSelfPermission(java.lang.String)' on a null object reference
E/AndroidRuntime(21981):    at id.flutter.plugins.FlutterBluetoothPrinterPlugin.ensurePermission(FlutterBluetoothPrinterPlugin.java:127)
E/AndroidRuntime(21981):    at id.flutter.plugins.FlutterBluetoothPrinterPlugin.startDiscovery(FlutterBluetoothPrinterPlugin.java:146)
E/AndroidRuntime(21981):    at id.flutter.plugins.FlutterBluetoothPrinterPlugin.access$100(FlutterBluetoothPrinterPlugin.java:39)
E/AndroidRuntime(21981):    at id.flutter.plugins.FlutterBluetoothPrinterPlugin$1.onReceive(FlutterBluetoothPrinterPlugin.java:83)
E/AndroidRuntime(21981):    at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1786)
E/AndroidRuntime(21981):    ... 8 more
I/Process (21981): Sending signal. PID: 21981 SIG: 9
Lost connection to device.
hungthinhitj commented 2 months ago

the cause of this crash is on the line "at id.flutter.plugins.FlutterBluetoothPrinterPlugin.ensurePermission(FlutterBluetoothPrinterPlugin.java:127)" it says activity.checkSelfPermission called when activity is null So, the solution is add a activity nullable guard like this to the package code image

minhtritc97 commented 2 months ago

I have created pull request to fix this issue, thank @hungthinhitj Check it please @ekasetiawans