Search code examples
androidbluetooth-lowenergycore-bluetoothandroid-bluetoothbluetooth-gatt

BLE connection error on Android 6 (Marshmallow) - unable to marshal value BluetoothGattCharacteristic


I am developing an Android app that works with Android Bluetooth LE. I was testing mostly on the newer devices with API 27 and above. The connection worked just fine. I was able to scan and connect to advertising devices.

Recently, I got a chance to test on Android 6.0 and Android 6.0.1 and things went south. I got the runtime exception with BluetoothGattCharacteristic and I am unsure why its not able to amrshal the value just for Android 6.0 and 6.0.1

I am handling some BLE notifications with a BroadcastReceiver which I am registering and unregistering at the right lifecycle callbacks.

mNotificationReceiver = new NotificationReceiver(mContext);
mNotificationReceiver.subscribeForBleManagerNotifications(mBleScannerCallback);
mNotificationReceiver.subscribeForPeripheralNotifications(mPeripheralCallback);
mNotificationReceiver.subscribeForGattNotifications(mGattCallback);
mNotificationReceiver.subscribeForErrorNotifications(mErrorCallback);

Here is the crash log.

    --------- beginning of crash
04-14 09:56:56.209 16432-16432/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mattel.mcpp.unity, PID: 16432
    java.lang.Error: FATAL EXCEPTION [main]
    Unity version     : 2019.2.9f1
    Device model      : LGE Nexus 5
    Device fingerprint: google/hammerhead/hammerhead:6.0.1/M4B30Z/3437181:user/release-keys

    Caused by: java.lang.RuntimeException: Parcel: unable to marshal value android.bluetooth.BluetoothGattCharacteristic@c829775
        at android.os.Parcel.writeValue(Parcel.java:1418)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
        at android.os.Bundle.writeToParcel(Bundle.java:1079)
        at android.os.Parcel.writeBundle(Parcel.java:711)
        at android.content.Intent.writeToParcel(Intent.java:7793)
        at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3044)
        at android.app.ContextImpl.sendBroadcast(ContextImpl.java:767)
        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:396)
        at com.mcpp.mattel.blekit.notification.Notifier$1.run(Notifier.java:141)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-14 09:56:56.211 780-1356/? W/ActivityManager:   Force finishing activity com.mattel.mcpp.unity/.UnityPlayerActivity
04-14 09:56:56.234 15784-15826/? W/bt_l2cap: l2cble_set_fixed_channel_tx_data_length, request not supported
04-14 09:56:56.234 15784-15814/? D/BtGatt.GattService: onConfigureMTU() address=03:CC:8F:0F:B3:82, status=0, mtu=247
04-14 09:56:56.235 16432-16442/? D/BluetoothGatt: onConfigureMTU() - Device=03:CC:8F:0F:B3:82 mtu=247 status=0
04-14 09:56:56.353 780-879/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-14 09:56:56.425 780-789/? I/art: Background partial concurrent mark sweep GC freed 35527(2MB) AllocSpace objects, 18(648KB) LOS objects, 33% free, 31MB/47MB, paused 2.107ms total 189.524ms
04-14 09:56:56.444 780-1410/? D/NetlinkSocketObserver: NeighborEvent{elapsedMs=1770229, 192.168.1.1, [3C37868CF82F], RTM_NEWNEIGH, NUD_REACHABLE}
04-14 09:56:56.747 780-793/? W/ActivityManager: Activity pause timeout for ActivityRecord{a047d29 u0 com.mattel.mcpp.unity/.UnityPlayerActivity t116 f}
04-14 09:56:59.128 1575-17219/? W/IcingInternalCorpora: getNumBytesRead when not calculated.
04-14 09:56:59.173 16432-16432/? I/Process: Sending signal. PID: 16432 SIG: 9
04-14 09:56:59.176 1366-1375/? I/art: Background partial concurrent mark sweep GC freed 81016(5MB) AllocSpace objects, 54(1252KB) LOS objects, 39% free, 24MB/40MB, paused 1.778ms total 103.500ms
04-14 09:56:59.199 1575-2165/? I/Icing: Usage reports 0 indexed 0 rejected 0 imm upload false
04-14 09:56:59.205 202-809/? D/audio_hw_primary: out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2
04-14 09:56:59.210 2855-2855/? W/Binder_B: type=1400 audit(0.0:550): avc: denied { ioctl } for path="socket:[116533]" dev="sockfs" ino=116533 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
04-14 09:56:59.210 2855-2855/? W/Binder_B: type=1400 audit(0.0:551): avc: denied { ioctl } for path="socket:[116533]" dev="sockfs" ino=116533 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
04-14 09:56:59.247 780-874/? W/InputDispatcher: channel 'cdef40d com.mattel.mcpp.unity/com.mattel.mcpp.unity.UnityPlayerActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
04-14 09:56:59.247 780-874/? E/InputDispatcher: channel 'cdef40d com.mattel.mcpp.unity/com.mattel.mcpp.unity.UnityPlayerActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-14 09:56:59.251 780-3556/? D/ConnectivityService: ConnectivityService NetworkRequestInfo binderDied(NetworkRequest [ id=35, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN] ], android.os.BinderProxy@b9acccf)
04-14 09:56:59.251 780-1778/? I/WindowState: WIN DEATH: Window{cdef40d u0 com.mattel.mcpp.unity/com.mattel.mcpp.unity.UnityPlayerActivity}
04-14 09:56:59.251 780-1778/? W/InputDispatcher: Attempted to unregister already unregistered input channel 'cdef40d com.mattel.mcpp.unity/com.mattel.mcpp.unity.UnityPlayerActivity (server)'
04-14 09:56:59.251 780-1778/? W/WindowManager: Force-removing child win Window{26a765c u0 SurfaceView} from container Window{cdef40d u0 com.mattel.mcpp.unity/com.mattel.mcpp.unity.UnityPlayerActivity}
04-14 09:56:59.251 15784-15800/? D/BtGatt.GattService: Binder is dead - unregistering client (5)!
04-14 09:56:59.252 780-891/? D/ConnectivityService: releasing NetworkRequest NetworkRequest [ id=35, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN] ]
04-14 09:56:59.253 780-891/? E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=35, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN] ]
04-14 09:56:59.255 15784-15816/? D/BtGatt.AdvertiseManager: message : 1
04-14 09:56:59.255 15784-15816/? D/BtGatt.AdvertiseManager: stop advertise for client 5
04-14 09:56:59.257 15784-15816/? D/BtGatt.GattService: onAdvertiseInstanceDisabled() - clientIf=5, status=0
04-14 09:56:59.257 15784-15816/? D/BtGatt.GattService: Client app is not null!
04-14 09:56:59.258 15784-15816/? D/BtGatt.AdvertiseManager: failed onAdvertiseInstanceDisabled
    android.os.DeadObjectException
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(Binder.java:503)
        at android.bluetooth.IBluetoothGattCallback$Stub$Proxy.onMultiAdvertiseCallback(IBluetoothGattCallback.java:882)
        at com.android.bluetooth.gatt.GattService.onAdvertiseInstanceDisabled(GattService.java:1298)
        at com.android.bluetooth.gatt.AdvertiseManager$AdvertiseNative.stopAdvertising(AdvertiseManager.java:318)
        at com.android.bluetooth.gatt.AdvertiseManager$ClientHandler.handleStopAdvertising(AdvertiseManager.java:212)
        at com.android.bluetooth.gatt.AdvertiseManager$ClientHandler.handleMessage(AdvertiseManager.java:175)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.os.HandlerThread.run(HandlerThread.java:61)
04-14 09:56:59.258 15784-15816/? D/BtGatt.AdvertiseManager: app died - unregistering client : 5
04-14 09:56:59.259 15784-15816/? D/BtGatt.GattService: unregisterClient() - clientIf=5
04-14 09:56:59.260 15784-15826/? W/bt_btif: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016
04-14 09:56:59.260 15784-15826/? W/bt_btif: bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0016
04-14 09:56:59.260 15784-15826/? W/bt_btif: bta_gattc_conn_cback() - cif=5 connected=0 conn_id=5 reason=0x0016
04-14 09:56:59.266 780-790/? W/WindowManager: Failed looking up window
    java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@92228e6 does not exist
        at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8743)
        at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8734)
        at com.android.server.wm.WindowState$DeathRecipient.binderDied(WindowState.java:1209)
        at android.os.BinderProxy.sendDeathNotice(Binder.java:558)
04-14 09:56:59.266 780-790/? I/WindowState: WIN DEATH: null

Please let me know if you have any possible idea or if you have faced this issue before. Your help is much appreciated. Thank you.


Solution

  • The older versions of Android is not able to marshal BluetoothGattCharacteristic even though it implements Parcelable. I was wrapping it in intent bundle with putExtra(). To support older versions (in my case Android 6.0), we need to pass string value of UUIDs if we are using intent bundles to pass around UUID for scan filters.

    Example, intent.putExtra(BleUtils.CHARACTERISTIC, characteristic.getUuid().toString());