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.
EDIT: Actually i found the exception, it is
Caused by: java.lang.SecurityException: Need BLUETOOTH_PRIVILEGED
permission: Neither user 10282 nor current process has
android.permission.BLUETOOTH_PRIVILEGED
So, google just hided the API it seems, so how can i connec with PAN to my radio?
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:
connect with "00000000-0000-1000-8000-00805f9b34fb" - default UUID
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);
- I checked all SO questions and I think the whole internet, but still there is no another solutions. What can be done to try?
EDIT 2:
When i check internet access in bluetooth settings in my device, it connects fine and i can use my application, but i cant connect programmaticly or even havent found how to set this checkbox or pop up this window for user