【问题标题】:Bluetooth Automatic Connection with Paired Devices与配对设备的蓝牙自动连接
【发布时间】:2011-09-23 10:10:36
【问题描述】:

我是新来的,看了很多你的帖子,还是没有找到解决问题的办法。

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

我想要做的是,一旦 Android 平板电脑(顺便说一下,整个通信中的主设备)检测到已知配对设备之一在范围内,就自动与终端设备连接。

一种可能性是不断轮询并尝试查看谁在我附近,但这会消耗大量电池寿命,并且如果我使用其中一个终端设备进入范围内并且我的 Android 平板电脑不在在轮询过程的中间,我不会获得自动连接;我必须等到下一个轮询周期。

这个问题有什么解决办法吗?

我希望整个事情都像 BT 耳机和我的得心应手一样工作:-/

感谢您的回答,希望我们能处理好!

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    我不确定这个解决方案是否有效。这个想法是获取所有配对的设备并循环通过它并尝试使用该设备的 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();
            }
         }
    

    【讨论】:

      猜你喜欢
      • 2015-10-26
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      相关资源
      最近更新 更多