蓝牙自动连接已配对设备

7
我是新来的,在阅读了很多您的帖子后,仍然没有找到解决我的问题的方法。
我正在编写一个使用蓝牙连接到终端设备的Android 2.2应用程序。我有一份已配对设备列表,并且可以将我的Android平板电脑与每个已知设备连接起来。
我想做的是,当Android平板电脑(顺便说一句,它是整个通信中的主机)检测到某个已知已配对设备在范围内时,自动连接到终端设备。
一种可能性是不断地轮询并尝试查看谁在我附近,但这会消耗大量电池寿命,并且如果我与其中一个终端设备进入范围,并且我的Android平板电脑不在轮询过程中,我将无法获得自动连接;我必须等待下一个轮询周期。
有没有解决这个问题的方法?
我希望整个过程能像BT耳机和我的手机一样工作 :-/
感谢您的答案,希望我们能解决它!
1个回答

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();
        }
     }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接