trobanga / flutter_btleplug

BSD 3-Clause "New" or "Revised" License
29 stars 9 forks source link

Unhandled Exception: FfiException(PANIC_ERROR, Runtime not initialized, null) #3

Closed Avani-A-S closed 1 year ago

Avani-A-S commented 1 year ago

I am using this app to study the setup for my app to support bluethooth using run btleplug. Facing below exception while running this app.

PS C:\Github\repo\btleplugtest\example> flutter run --release

Running Gradle task 'assembleRelease'...                           62.7s
√  Built build\app\outputs\flutter-apk\app-release.apk (18.7MB).
Installing build\app\outputs\flutter-apk\app.apk...                 3.2s

Flutter run key commands.
h List all available interactive commands.
c Clear the screen
q Quit (terminate the application on the device).
E/flutter (15849): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FfiException(PANIC_ERROR, Runtime not initialized, null)
E/flutter (15849): #0      FlutterRustBridgeBase._transformRust2DartMessage (package:flutter_rust_bridge/src/basic.dart:89)
E/flutter (15849): #1      FlutterRustBridgeBase.executeStream (package:flutter_rust_bridge/src/basic.dart:73)
E/flutter (15849): <asynchronous suspension>
E/flutter (15849): #2      main.<anonymous closure> (package:btleplugtest_example/main.dart:13)
E/flutter (15849): <asynchronous suspension>
E/flutter (15849):

For local setup changes I have done are as below:

  1. Locally check out btleplug, jni-rs & jni-util-s and changed the path of these directories in native/Cargo.toml dependencies.
  2. Updated btleplugtest\example\android\app\build.gradle and added proguard-rules.pro at same location as per https://crates.io/crates/btleplug#android.
  3. Updated manifest for Bluetooth permissions as below.
    <uses-permission android:name="android.permission.BLUETOOTH"
        android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
        android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  4. Testing this app on mobile device • android-arm64 • Android 12 (API 31)
  5. Another issue I am facing is without --release getting permission error as below.
PS C:\Github\repo\btleplugtest\example> flutter run
Launching lib\main.dart on SM M317F in debug mode...
Running Gradle task 'assembleDebug'...                             36.2s
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...                11.5s
E/AndroidRuntime(21448): FATAL EXCEPTION: Thread-14
E/AndroidRuntime(21448): Process: com.example.btleplugtest_example, PID: 21448
E/AndroidRuntime(21448): java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission for AttributionSource { uid = 10434, packageName = com.example.btleplugtest_example, attributionTag = null, token = android.os.BinderProxy@3385ce2, next = null }: GattService registerScanner
E/AndroidRuntime(21448):        at android.os.Parcel.createExceptionOrNull(Parcel.java:2437)
E/AndroidRuntime(21448):        at android.os.Parcel.createException(Parcel.java:2421)
E/AndroidRuntime(21448):        at android.os.Parcel.readException(Parcel.java:2404)
E/AndroidRuntime(21448):        at android.os.Parcel.readException(Parcel.java:2346)
E/AndroidRuntime(21448):        at android.bluetooth.IBluetoothGatt$Stub$Proxy.registerScanner(IBluetoothGatt.java:1886)
E/AndroidRuntime(21448):        at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper.startRegistration(BluetoothLeScanner.java:519)
E/AndroidRuntime(21448):        at android.bluetooth.le.BluetoothLeScanner.startScan(BluetoothLeScanner.java:305)
E/AndroidRuntime(21448):        at android.bluetooth.le.BluetoothLeScanner.startScan(BluetoothLeScanner.java:161)
E/AndroidRuntime(21448):        at com.nonpolynomial.btleplug.android.impl.Adapter.startScan(:19)
trobanga commented 1 year ago

Have a look here: https://github.com/deviceplug/btleplug/issues/272#issuecomment-1287899336

I've also just pushed my latest version that should work (I hope).

Avani-A-S commented 1 year ago

This doesn't seem to be working for me. Facing same errors for debug & release. Working for you? image

trobanga commented 1 year ago

I'm sorry but I think I gave you the solution to another problem...

I just checked and the current code in example/ works on my phone. Have you checked the app's settings on your device? It needs permission for nearby devices?

Avani-A-S commented 1 year ago

Yes. Gave both permissions - nearby devices and location, in the app's settings on my device and its working. Thanks a lot for your response!