如何在Android的编程方式检查蓝牙绑定状态蓝牙、绑定、状态、方式

2023-09-12 08:15:25 作者:且笑天下江山如画

有没有办法找出蓝牙绑定已启用或禁用的Andr​​oid 2.3+编程(后2.3 +任何版本)?

Is there any way to find out bluetooth tethering is enabled or disabled programmatically in Android 2.3+(any version after 2.3+)?

我没有要求启用/禁用它,但只知道它是当前已打开与否。

I am not asking to enable/disable it, but only to know if it is enabled currently or not.

推荐答案

原来,BluetoothPan(个人区域网络)的关键字圈养。我凝视着API文档和Android源$ C ​​$ C,但在评论中简短的例子是误导性的。这张海报提供了一个例子,但我不得不与它最初的麻烦: Android BluetoothPAN创建的Andr​​oid设备和Windows7 PC TCP / IP网络

It turns out that BluetoothPan (Personal Area Networking) is the keyword for tethering. I pored over the API documentation and Android source code, but the brief examples in the comments were misleading. This poster provided an example but I had trouble with it initially: Android BluetoothPAN to create TCP/IP network between Android device and Windows7 PC

我尝试了各种其它方法,包括检查对BT设备的IP地址。但是没有蓝牙网络设备不存在,所以没有IP检查。 检测USB绑定在Android

I tried various other methods including checking the IP address of the BT device. However no Bluetooth network device exists, so there's no IP to check. Detect USB tethering on android

回到BluetoothPan code ...在第一线的例子是不完整的(没有的ServiceListener实现)。我想一个标准之一,但 isTetheringOn 的代理呼叫失败。的关键部分是, onServiceConnected()的回调需要至少一行code或编译器优化它拿走。它应该也不会断开代理像大多数其他的例子都有。这里的工作code:

Back to the BluetoothPan code... The example in the first thread was incomplete (no ServiceListener implementation). I tried a standard one but the isTetheringOn proxy call failed. The crucial piece is that the onServiceConnected() callback needs at least one line of code or the compiler optimizes it away. It also shouldn't disconnect the proxy like most other examples have. Here's the working code:

BluetoothAdapter mBluetoothAdapter = null;
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Class<?> noparams[] = {};
Method mIsBTTetheringOn;

@Override
public void onCreate() {
    Context MyContext = getApplicationContext();
    mBluetoothAdapter = getBTAdapter();
    try {
        classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");
        mIsBTTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", noparams);
        BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
        BTPanCtor.setAccessible(true);
        BTSrvInstance = BTPanCtor.newInstance(MyContext, new BTPanServiceListener(MyContext));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private BluetoothAdapter getBTAdapter() {
    if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
        return BluetoothAdapter.getDefaultAdapter();
    else {
        BluetoothManager bm = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        return bm.getAdapter();
    }
}

// Check whether Bluetooth tethering is enabled.
private boolean IsBluetoothTetherEnabled() {
    try {
        if(mBluetoothAdapter != null) {
            return (Boolean) mIsBTTetheringOn.invoke(BTSrvInstance, (Object []) noparams);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

public class BTPanServiceListener implements BluetoothProfile.ServiceListener {
    private final Context context;

    public BTPanServiceListener(final Context context) {
        this.context = context;
    }

    @Override
    public void onServiceConnected(final int profile,
                                   final BluetoothProfile proxy) {
        //Some code must be here or the compiler will optimize away this callback.
        Log.i("MyApp", "BTPan proxy connected");
    }

    @Override
    public void onServiceDisconnected(final int profile) {
    }
}