Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android crash when stopScanning is called with switched off BLE adapter #317

Closed
squio opened this issue Apr 13, 2022 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@squio
Copy link
Contributor

squio commented Apr 13, 2022

Describe the bug
Calling stopLEScan() when no BleAdapter state is OFF crashes with IllegalStateException

To Reproduce

  • Create script which calls BleClient.requestLEScan()
  • Add BleClient.stopLEScan(); in a timeout or call it manually

I was not able to reproduce this myself but got crash logs from different devices in Android Play console; unfortunately no user reports 😢
See below for devices and crash logs

Expected behavior
Call to stopLEScan() will not cause an error

Screenshots
If applicable, add screenshots to help explain your problem.

Plugin version:

  • @capacitor-community/bluetooth-le: 1.8.0

Desktop (please complete the following information):

  • N/A

Smartphone (please complete the following information):

  • Device: Motorola edge 20 /Fairphone3
  • OS: Android 11 / Android 12
  • Browser: System WebView
  • Version N/A

Additional context

Full crash report (Motorola device, Android 11):

java.lang.RuntimeException: 
  at com.getcapacitor.Bridge.lambda$callPluginMethod$0$com-getcapacitor-Bridge (Bridge.java:601)
  at com.getcapacitor.Bridge$$ExternalSyntheticLambda5.run (Unknown Source:8)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:250)
  at android.os.HandlerThread.run (HandlerThread.java:67)
Caused by: java.lang.reflect.InvocationTargetException: 
  at java.lang.reflect.Method.invoke (Native Method)
  at com.getcapacitor.PluginHandle.invoke (PluginHandle.java:121)
  at com.getcapacitor.Bridge.lambda$callPluginMethod$0$com-getcapacitor-Bridge (Bridge.java:592)
Caused by: java.lang.IllegalStateException: 
  at android.bluetooth.le.BluetoothLeUtils.checkAdapterStateOn (BluetoothLeUtils.java:177)
  at android.bluetooth.le.BluetoothLeScanner.stopScan (BluetoothLeScanner.java:285)
  at com.capacitorjs.community.plugins.bluetoothle.DeviceScanner.stopScanning (DeviceScanner.kt:138)
  at com.capacitorjs.community.plugins.bluetoothle.BluetoothLe.stopLEScan (BluetoothLe.kt:356)

I can submit a PR with the following code if you agree this would be a good solution:

In BluetoothLe.kt, line 353:

    @PluginMethod
    fun stopLEScan(call: PluginCall) {
        assertBluetoothAdapter(call) ?: return
        try {
            deviceScanner?.stopScanning()
        }
        catch (e: IllegalStateException) {
            Log.e(TAG, "Error in stopLEScan: ${e.localizedMessage}")
        }
        call.resolve()
    }
@squio squio added the bug Something isn't working label Apr 13, 2022
squio added a commit to squio/bluetooth-le that referenced this issue Apr 13, 2022
@pwespi
Copy link
Member

pwespi commented Apr 15, 2022

closed by #318

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants