16

I'm suppose to write an application that will act as the bluetooth client. What I'm trying to do is figure out is what would be the best way to figure out if the specific device that I'm supporting is in range without attempting to do a BluetoothDevice.connect() on it all of the time and failing if it's not in range. Here we're assuming that the device as already been paired.

I'm afraid that it would be bad practice to attempt to connect to a specific device all of the time until it's in range. Seems to me that it would be bad for the battery.

Would anyone know of any methods or concepts that should be used to accomplish what I'm trying to do?

Thanks!

4

3 回答 3

1

好的,所以我从你的问题中了解到,你想得到你配对的设备的名称,如果它在范围内?

所以这里是解决方案: -

  1. 创建一个名为 DeviceDetails 的类:

类 DeviceDetails{ 公共字符串名称;公共字符串地址;公共蓝牙设备远程设备;}

  1. 您需要按照此处的说明连接和配对您的设备,一旦完成并建立连接,请创建一个 DeviceDetails 对象。

  2. DeviceDetails selectedDevice;如果您有一个自定义适配器来显示设备列表,则将视图中的位置传递给 selectedDevice 引用。例子 :-selectedDevice = adapter.getItem(pos);

  3. 现在您有了已选择配对的 SelectedDevice 对象,因此您可以将其地址和名称保存在

    喜好。

    Example:- 
    savePairedDeviceInfo(selectedDevice.name, selectedDevice.address);
        public void savePairedDeviceInfo(String name, String addr)
            {
                if(name != null && !name.equalsIgnoreCase(""))
                    editor.putString(PNAME_OF_DEVICE, name);
    
                if(addr != null && !addr.equalsIgnoreCase(""))
                    editor.putString(MAC_ADDRESS_OF_DEVICE, addr);
    
                editor.commit();
            }
    
  4. 现在,下次当您想要检查配对设置是否已完成或不通过从首选项中获取值来检查设备的名称和地址时。使用检查(如果条件)返回 true 配对的旧设备是相同的或其他的。

  5. 如果配对设备在范围内,您将获得相同的值,否则它将尝试与其他设备配对。

如果您从我的解释中理解,请告诉我。

于 2013-06-17T11:09:23.273 回答
1

您可以按照以下方式:

  1. 从蓝牙适配器获取配对设备列表
  2. 开始发现
  3. 当找到设备时,假设设备 A,您可以检查设备 A 是否在配对设备列表中。如果设备 A 是配对设备之一,则表示它是范围内配对的可用设备。
于 2016-11-10T09:22:14.227 回答
0

如果发现新设备,则启动设备发现并检查 BluetoothDevice BONDED 状态。

val isBonded = discoveredDevice.bondState == BluetoothDevice.BOND_BONDED
于 2021-03-25T12:56:21.080 回答