Android13中反射调用createRfcommSocket方法失效问题求助
编辑补充
- 经过调研,使用蓝牙功能需在清单文件中添加以下权限:
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
- 原
requestPermissions方法不再生效,改用registerForActivityResult结合requestMultiplePermissions申请权限,代码示例:
String[] permissionsArray = new String[] { Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.READ_MEDIA_AUDIO, Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE, }; requestMultiplePermissions.launch(permissionsArray);
- 检查权限状态的代码:
private ActivityResultLauncher<String[]> requestMultiplePermissions = registerForActivityResult( new ActivityResultContracts.RequestMultiplePermissions(), permissions -> { for (Map.Entry<String, Boolean> entry : permissions.entrySet()) { String key = entry.getKey(); Boolean value = entry.getValue(); Log.d(TAG, key + " = " + value); } } );
问题详情
从Android 9开始使用以下反射方法连接指定通道(22)的蓝牙设备:
try { Method m = device.getClass().getMethod("createRfcommSocket", int.class); socket = (BluetoothSocket) m.invoke(device, 22); } catch (Exception e) { LogHelper.e(TAG, "Socket create() failed", e); }
Android 12下的正常日志
2023-06-14 18:48:04.403 12055-18866/? D/y: [d] > ConnectToBluetoothSocket attempt number 0 to Device: xx:xx:xx:xx:c0:09 2023-06-14 18:48:04.414 12328-12423/? I/bluetooth: system/bt/stack/rfcomm/rfc_mx_fsm.cc:79 rfc_mx_sm_execute: RFCOMM peer:xx:xx:xx:xx:c0:09 event:6 state:idle 2023-06-14 18:48:11.993 3402-3752/? I/BistoWorker: (REDACTED) bluetoothConnectionStateChanged. Action: %s 2023-06-14 18:48:12.156 12328-12423/? I/bluetooth: system/bt/stack/rfcomm/rfc_mx_fsm.cc:79 rfc_mx_sm_execute: RFCOMM peer:xx:xx:xx:xx:c0:09 event:9 state:wait_config 2023-06-14 18:48:12.157 12328-12423/? I/bluetooth: system/bt/stack/rfcomm/rfc_mx_fsm.cc:79 rfc_mx_sm_execute: RFCOMM peer:xx:xx:xx:xx:c0:09 event:12 state:configure 2023-06-14 18:48:12.157 12328-12423/? I/bluetooth: system/bt/stack/rfcomm/rfc_mx_fsm.cc:79 rfc_mx_sm_execute: RFCOMM peer:xx:xx:xx:xx:c0:09 event:11 state:configure 2023-06-14 18:48:12.218 12328-12423/? I/bluetooth: system/bt/stack/rfcomm/rfc_mx_fsm.cc:79 rfc_mx_sm_execute: RFCOMM peer:xx:xx:xx:xx:c0:09 event:1 state:sabme_wait_ua 2023-06-14 18:48:12.269 12328-12423/? I/bluetooth: system/bt/bta/dm/bta_dm_pm.cc:434 bta_dm_pm_cback: New connection service:Unknown[26] app_id:1 2023-06-14 18:48:12.270 12328-12423/? I/bluetooth: system/bt/bta/dm/bta_dm_pm.cc:826 bta_dm_pm_ssr: Found connected service:Unknown app_id:1 peer:xx:xx:xx:xx:c0:09 spec_name:sniff_capable 2023-06-14 18:48:12.270 12328-12423/? I/bluetooth: system/bt/bta/dm/bta_dm_pm.cc:905 bta_dm_pm_active: Active power mode already set for device:xx:xx:xx:xx:c0:09 2023-06-14 18:48:12.274 12328-12423/? I/bluetooth: system/bt/bta/dm/bta_dm_pm.cc:905 bta_dm_pm_active: Active power mode already set for device:xx:xx:xx:xx:c0:09 2023-06-14 18:48:12.360 12328-12423/? I/bluetooth: system/bt/bta/dm/bta_dm_pm.cc:905 bta_dm_pm_active: Active power mode already set for device:xx:xx:xx:xx:c0:09 2023-06-14 18:48:12.363 12328-12423/? I/bluetooth: system/bt/bta/dm/bta_dm_pm.cc:905 bta_dm_pm_active: Active power mode already set for device:xx:xx:xx:xx:c0:09 2023-06-14 18:48:12.365 12328-12423/? I/bluetooth: system/bt/bta/dm/bta_dm_pm.cc:905 bta_dm_pm_active: Active power mode already set for device:xx:xx:xx:xx:c0:09 2023-06-14 18:48:12.366 12055-18866/? D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@181bb2d, channel: 22, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@d129862, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@924edf3mSocket: android.net.LocalSocket@22b8ab0 impl:android.net.LocalSocketImpl@4ebdd29 fd:java.io.FileDescriptor@c85d8ae, mSocketState: CONNECTED
Android 13下的异常日志
2023-06-14 18:56:35.313 23528-23528/? D/e: [d] > Bluetooth discovery finished - deviceFound: true 2023-06-14 18:56:35.314 23528-23528/? D/e: [d] > Bluetooth discovery finished - deviceFound: true 2023-06-14 18:56:37.298 23528-23715/? D/y: [d] > connect Socket using bluetooth 2023-06-14 18:56:37.298 23528-23715/? D/y: [d] > ConnectToBluetoothSocket attempt number 0 to Device: xx:xx:xx:xx:c0:09 2023-06-14 18:56:37.299 23528-23715/? I/BluetoothSocket: connect() for device 3414B5_9 called by pid: 23528 2023-06-14 18:56:38.302 689-2264/? E/bt_upio: upio_set : /proc/bluetooth/sleep/btwrite(-1) incorrent 2023-06-14 18:57:16.809 1122-1210/? D/BatteryExternalStatsWorker: Fetch Bluetooth data 2023-06-14 18:57:16.811 689-689/? E/bt_upio: upio_set : /proc/bluetooth/sleep/btwrite(-1) incorrent 2023-06-14 18:57:16.817 1122-1210/? D/BatteryExternalStatsWorker: Update bluetooth state 2023-06-14 18:57:19.816 689-2264/? E/bt_upio: upio_set : /proc/bluetooth/sleep/btwrite(-1) incorrent
连接失败时的错误日志
E/bt_rfcomm: rfc mcb timeout on 1 E/bt_btif_sock_rfcomm: find_rfc_slot_by_id unable to find RFCOMM slot id: 9400 I/bt_stack: [INFO:port_rfc.cc(1036)] port_rfc_closed: RFCOMM connection closed, index=25, state=1, reason=Peer connection failed[16], UUID=0x1101, bd_addr=3414b5_9, is_server=0 I/bt_stack: [INFO:port_api.cc(220)] RFCOMM_CreateConnectionWithSecurity: bd_addr=3414b5_9, scn=22, is_server=0, mtu=990, uuid=0x1101, dlci=44, signal_state=0x0b, p_port=0x7df912a358
项目参数
compileSdkVersion 28 buildToolsVersion '28.0.3' minSdkVersion 23 targetSdkVersion 28 Android Gradle Plugin Version 3.1.0 Gradle Version 4.4
目前未找到类似案例,请问是否存在兼容性问题?希望找到无需升级targetSdkVersion的解决方案。
内容的提问来源于stack exchange,提问作者Wephyr
相关产品推荐
相关产品推荐

