Saturday, 1 May 2021

Bluetooth PAN Connect Android 11

My application is connecting to radio via Bluetooth as a PAN device. With new android 11 update this code does not work

String sClassName = "android.bluetooth.BluetoothPan";
final Class<?> classLocalBluetoothProfileManager = Class.forName(sClassName);
Constructor<?> ctor = classLocalBluetoothProfileManager.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
ctor.setAccessible(true);
Object instance = ctor.newInstance(getApplicationContext(), new BluetoothProfile.ServiceListener() {     
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
    Method connectMethod = null;
    try {
            connectMethod = proxy.getClass().getDeclaredMethod("connect", BluetoothDevice.class);
            if (((Boolean) connectMethod.invoke(proxy, device))) {
                            System.out.println(">>> ConnectPAN ->true");
                        } else {
                            System.out.println(">>> ConnectPAN ->false");
                        }
                    } catch (Exception e) {
                        System.out.println(">>> ConnectPAN ->ex " + e.getMessage());
                    }
                    BluetoothAdapter.getDefaultAdapter().closeProfileProxy(PAN, proxy);

     // some stuff with connect events

On 11 android I get null reference in try method, so may be this api is hidden starting from 11 android.

So i opened docs and tried to connect with Bluetooth socket:

The idea is described here. I dont think i need to rewrite this sample code, but what i've tried:

  1. connect with "00000000-0000-1000-8000-00805f9b34fb" - default UUID

  2. connect with custom uuids 00001116 and 00001112 with default and 1116 ID socket does not open with error

    unable to connect read failed, socket might closed or timeout, read ret: -1
    

when I use 1112 uuid the connection is establishing, but there is no PAN connect, so I don't get IP from device and "Internet access" checkbox in bluetooth device in settings is unchecked.

All these UUID is checked with secure, unsecure and via reflect method.

Also i tried creating socket with reflect and choosing channel, tried all 30 channels, sometimes connect was established but no PAN as well.

 socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
  1. I checked all SO questions and I think the whole internet, but still there is no another solutions. What can be done to try?


from Bluetooth PAN Connect Android 11

No comments:

Post a Comment