Wephyr
Wephyr

Reputation: 1

Is device.getClass().getMethod("createRfcommSocket", int.class); not working anymore on Android 13?

---- EDIT -----

After some research and stuff, It seems that I also needed permissions from the manifest in order to use the bluetooth.

<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>

Bluetooth Permissions

Also I don't know if it's going to be useful, I needed to change the way to ask permissions, requestPermissions was not working anymore so I switched to registerForActivityResult with a requestMultiplePermissions that helped a lot.

See the snippet of code to call your permissions, in the exemple it's to ask all the permission I need.

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);

You can see and check the permissions over there, if you need to do special tasks.

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);
                }
            }
    );

---- END EDIT -----

let me explain. I'm using this method to connect on a device on a specific channel since Android 9

           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);
        }

This log part, is from Android 12 and it's working good.

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

Now the same part but on 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

Sometimes it's impossible that the connection is set, some other time we have lines like this :

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

If anyone have an idea of what's going on ? I didn't find anything similar to my case on google..

I'm pointing out also another thing, do you think it's possible that it could be due to a retro compatibility issue?

My project parameters are :

compileSdkVersion 28
buildToolsVersion '28.0.3'
minSdkVersion 23
targetSdkVersion 28

Android Gradle Plugin Version 3.1.0
Gradle Version 4.4

Thanks in advance

It's a big project, I want to be sure that the problem can't be fixed. In last resort to migrate on superior target SDK

Upvotes: 0

Views: 269

Answers (0)

Related Questions