简体   繁体   English

蓝牙泛连接 Android 11

[英]Bluetooth PAN Connect Android 11

My application is connecting to radio via Bluetooth as a PAN device.我的应用程序通过蓝牙作为 PAN 设备连接到无线电。 With new android 11 update this code does not work使用新的 android 11 更新此代码不起作用

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.在 11 android 我在 try 方法中得到 null 参考,所以可能这个 api 从 11 ZC3FC.132364CE19CA8A 开始隐藏

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?所以,谷歌似乎只是隐藏了 API,那么我怎样才能将 PAN 连接到我的收音机?

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与“00000000-0000-1000-8000-00805f9b34fb”连接 - 默认 UUID

  2. connect with custom uuids 00001116 and 00001112 with default and 1116 ID socket does not open with error使用默认和 1116 ID 连接自定义 uuid 00001116 和 00001112 套接字未打开并出现错误

    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.当我使用 1112 uuid 时,连接正在建立,但没有 PAN 连接,所以我没有从设备获取 IP 并且未选中设置中蓝牙设备中的“互联网访问”复选框。

All these UUID is checked with secure, unsecure and via reflect method.所有这些 UUID 都通过安全、不安全和反射方法进行检查。

Also i tried creating socket with reflect and choosing channel, tried all 30 channels, sometimes connect was established but no PAN as well.我还尝试使用反射和选择通道创建套接字,尝试了所有 30 个通道,有时连接已建立但也没有 PAN。

 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.我检查了所有 SO 问题,我认为整个互联网,但仍然没有其他解决方案。 What can be done to try?有什么可以尝试的?

EDIT 2:编辑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当我在设备的蓝牙设置中检查互联网访问时,它连接正常,我可以使用我的应用程序,但我无法以编程方式连接,甚至没有找到如何设置此复选框或为用户弹出此 window

Yes, this api is in the greylist for Android 11.是的,这个 api 在 Android 11 的灰名单中。

But what I see in Android 11 sources: In the PanService (Bluetooth app) the connect method calls getConnectionState which is... blacklisted.但是我在 Android 11 个来源中看到的内容:在 PanService(蓝牙应用程序)中,连接方法调用 getConnectionState,它被列入黑名单。 So at the end this method should be considered as blacklisted.所以最后这个方法应该被视为黑名单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM