CodeMinion / another_brother

Another Brother Flutter SDK
BSD 3-Clause "New" or "Revised" License
21 stars 19 forks source link

App crash on Android, targetSdkVersion 33 when permissions are explicitly not accepted #75

Closed lillianho closed 7 months ago

lillianho commented 7 months ago

Hi,

We have been testing with this package and have found that if our physical Android device is on OS 13, and if the build is targeting Android sdk 33, without accepting the bluetooth_scan and bluetooth_connect permissions, our app will crash with the following exception:

I/BluetoothAdapter(24732): STATE_ON
D/BluetoothLeScanner(24732): could not find callback wrapper
E/gralloc4(24732): Empty SMPTE 2094-40 data
3
I/BluetoothAdapter(24732): STATE_ON
E/gralloc4(24732): Empty SMPTE 2094-40 data
I/BluetoothAdapter(24732): STATE_ON
D/BluetoothLeScanner(24732): Start Scan with callback
E/AndroidRuntime(24732): FATAL EXCEPTION: Thread-9
E/AndroidRuntime(24732): Process: com.appname.uat, PID: 24732
E/AndroidRuntime(24732): java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 10275, packageName = com.appname.uat, attributionTag = null, token = android.os.BinderProxy@fa5217c, next = null }: AdapterService getBondedDevices
E/AndroidRuntime(24732):    at com.android.bluetooth.Utils.checkPermissionForDataDelivery(Utils.java:923)
E/AndroidRuntime(24732):    at com.android.bluetooth.Utils.checkConnectPermissionForDataDelivery(Utils.java:955)
E/AndroidRuntime(24732):    at com.android.bluetooth.btservice.AdapterService$AdapterServiceBinder.getBondedDevices(AdapterService.java:2955)
E/AndroidRuntime(24732):    at com.android.bluetooth.btservice.AdapterService$AdapterServiceBinder.getBondedDevices(AdapterService.java:2947)
E/AndroidRuntime(24732):    at android.bluetooth.IBluetooth$Stub.onTransact(IBluetooth.java:1060)
E/AndroidRuntime(24732):    at android.os.Binder.execTransactInternal(Binder.java:1321)
E/AndroidRuntime(24732):    at android.os.Binder.execTransact(Binder.java:1280)
E/AndroidRuntime(24732): FATAL EXCEPTION: Thread-11
E/AndroidRuntime(24732): Process: com.appname.uat, PID: 24732
E/AndroidRuntime(24732): java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission for AttributionSource { uid = 10275, packageName = com.appname.uat, attributionTag = null, token = android.os.BinderProxy@fa5217c, next = null }: GattService registerScanner
E/AndroidRuntime(24732):    at com.android.bluetooth.Utils.checkPermissionForDataDelivery(Utils.java:923)
E/AndroidRuntime(24732):    at com.android.bluetooth.Utils.checkScanPermissionForDataDelivery(Utils.java:982)
E/AndroidRuntime(24732):    at com.android.bluetooth.gatt.GattService.registerScanner(GattService.java:3744)
E/AndroidRuntime(24732):    at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerScanner(GattService.java:977)
E/AndroidRuntime(24732):    at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerScanner(GattService.java:965)
E/AndroidRuntime(24732):    at android.bluetooth.IBluetoothGatt$Stub.onTransact(IBluetoothGatt.java:290)
E/AndroidRuntime(24732):    at android.os.Binder.execTransactInternal(Binder.java:1316)
E/AndroidRuntime(24732):    at android.os.Binder.execTransact(Binder.java:1280)
6
E/gralloc4(24732): Empty SMPTE 2094-40 data
I/Process (24732): Sending signal. PID: 24732 SIG: 9
Lost connection to device.
Exited

We tested using the latest version of this package: 2.1.1.

This is not an issue on physical Android devices that are below OS 13. We have also simulated the exact issue in https://github.com/CodeMinion/Demo-Another-Brother-Prime - we modified the pubspec to target the latest version of this package as well.

I do appreciate that permissions are required for bluetooth scanning to succeed.

However, we are only interested in looking for printers via WIFI and not bluetooth, so we don't want to make it mandatory for users to accept bluetooth permissions.

Also, if we explicitly decline the bluetooth_scan and bluetooth_connect permissions, the app also crashes in the same manner, which means our app doesn't work unless users explicitly agree to bluetooth permissions outright. The app doesn't crash in the case where users do explicitly accept bluetooth_scan and bluetooth_connect permissions.

Users should be able to decline whatever permissions they wish, and our app will respond to their permissions by allowing them or disallowing them to print instead of crashing the app, ideally.

This may be a limitation of the brother api rather than the intended behaviour of this package, so any suggestions about what can be done is much appreciated.

Many thanks, Lillian

CodeMinion commented 7 months ago

Hi @lillianho ,

Thanks for reaching out about this. Could I trouble you to share the code you are using for printing and finding the printers?

Based on the logs I see that the app is trying to check the paired devices as well as perform a BLE scan. None of these are performed unless the respective APIs are call.

Thanks in advance,

lillianho commented 7 months ago

Hello @CodeMinion,

Sorry, I just realised that the demo app had several pages - one of which had the wifi logic separate from the bluetooth one. I also just realised I could check permissions first before calling scan_bluetooth, so there is actually no problem.

Just another question, is there any way to do a search of all printers on the network without defining the Printer/ PaperInfo upfront?

We are trying to support as many wifi printers and paper (cut/uncut/dimensions) as we can (that fall under the list of another_brother's support), and we'd planned to scale the print job according to what paper installed in any printer.

Can we do a printer search that return all printers, regardless of model and get returned PaperInfo from the discovered printers so that we don't have to define that ourselves?

If there is no way to do so, what would be the best way to implement another_brother to support as many paper/printers as possible?

Many thanks, Lillian

CodeMinion commented 7 months ago

Hi @lillianho ,

Glad you were able to find the cause.

With regards to finding the printers, you could use the printers list and pass it to the search function. I have not tested with so many printers myself so can't say from experience whether it will work as intended. https://github.com/CodeMinion/another_brother/blob/main/lib/printer_info.dart#L385

As for the paper, only some of the Brother models support querying the paper installed in the printer. If I remember correctly it's something mainly the QL and PT printers can do. https://support.brother.co.jp/j/s/support/html/mobilesdk/reference/android/printer.html#getlabelinfo

The approach I have normally seen others follow is to have a way for the users to select the correct paper option based on the printer. Here is an example project for configuring printer and the papers based on the selected printer: https://github.com/CodeMinion/Printer-Configuration-UI

Thanks again for reaching out. Hope this helps but don't hesitate to reach out anytime if there is anything else I can help with,