Android 6 (Marshmallow) 上的 BLE 连接错误 - 无法编组值 BluetoothGattCharacteristic

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

我正在开发一个与 Android 低功耗蓝牙配合使用的 Android 应用程序。我主要在 API 27 及更高版本的较新设备上进行测试。连接工作得很好。我能够扫描并连接到广告设备。

最近,我有机会在 Android 6.0 和 Android 6.0.1 上进行测试,但事情进展顺利。我有 BluetoothGattCharacteristic 的运行时异常,我不确定为什么它不能仅针对 Android 6.0 和 6.0.1

amrshal 值

我正在使用 BroadcastReceiver 处理一些 BLE 通知,我在正确的生命周期回调中注册和取消注册。

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

这是崩溃日志。

    --------- 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.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

如果您有任何可能的想法或者您以前是否遇到过这个问题,请告诉我。非常感谢您的帮助。谢谢。

Android 的旧版本无法编组 BluetoothGattCharacteristic,即使它 implements Parcelable。我用 putExtra() 将它包装在意图包中。为了支持旧版本(在我的例子中 Android 6.0),如果我们使用意图包为扫描过滤器传递 UUID,我们需要传递 UUID 的字符串值。

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