蓝牙与配对设备的自动连接

Fra*_*Mir 7 android bluetooth

我是新来的,我已经阅读了很多你的帖子,但仍然没有找到问题的解决方案.

我正在为Android 2.2编写一个使用蓝牙连接到终端设备的应用程序.我有一个配对设备列表,我可以将我的Android平板电脑与我已知的每个设备连接起来.

我想做的是,只要Android-Tablet(顺便说一下整个通信中的Master)检测到其中一个已知的配对设备在范围内,就会自动连接终端设备.

一种可能性是不断轮询并试图看到谁在我附近,但是这会花费很多电池寿命,如果我使用其中一个终端设备进入范围而我的Android平板电脑不在中间轮询过程,我不会得到自动连接; 我不得不等到下一个轮询周期.

这个问题有什么解决方案吗?

我会像BT-Headsets和我的方便一样工作: - /

感谢您的回答,希望我们能够解决它!

Rag*_*dra 0

我不确定这个解决方案是否有效。这个想法是获取所有配对的设备并循环它并尝试使用该设备的 MAC 地址进行连接

    String macAddress;     
    for (BluetoothDevice device : pairedDevices) {

    BluetoothSocket bluetoothSocket = null;
    try {
        if (bluetoothSocket == null || !bluetoothSocket.isConnected()) {
        bluetoothSocket = device.createRfcommSocketToServiceRecord(MYUUID);
        mBluetoothAdapter.cancelDiscovery();
        if(!bluetoothSocket.isConnected()){
            bluetoothSocket.connect();
        }
        if (bluetoothSocket.getInputStream() != null && bluetoothSocket.getOutputStream() != null) {
            macAddress = device.getAddress();
        }
     }
Run Code Online (Sandbox Code Playgroud)