Android 上的蓝牙 - 如何连接到正确的蓝牙设备?
我正在编写一个通过 rfcomm 与外部附件通信的程序。 我的问题是我不知道识别我的设备的正确方法是什么。 我现在这样做的方式是这样的:
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals(MY_DEVICE_NAME)) {
this.myDevice = device;
break;
}
}
然而,这种方法依赖于设备的名称,这对我来说似乎又脏又糟糕:) 有更好的方法吗? 我尝试查看 BluetoothDevice 但似乎没有任何帮助 - 这个名字真的是最好的方法吗? 我看到在某些地方人们说我应该使用 UUID,但它用于在我拥有设备后打开设备的套接字:
_socket = myDevice.createRfcommSocketToServiceRecord(MY_UUID);
有更好的方法吗?
I'm writing a program that speaks with an external accessory over rfcomm.
My problem is that I don't know what the correct way of identifying my device is.
the way I do it now is like this:
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals(MY_DEVICE_NAME)) {
this.myDevice = device;
break;
}
}
This method however relies on the name of the device which to me seems dirty and bad :)
is there a better way to do this?
I tried looking at all the methods of BluetoothDevice but none seemed to help - is the name really the best way to do it?
I saw that in some places people say that I should use UUIDs but that is used to open the socket to the device once I have it:
_socket = myDevice.createRfcommSocketToServiceRecord(MY_UUID);
is there a better way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
相同种类/功能和/或品牌的设备通常具有相似的名称。例如,所有 Roving Networks 的 RN-41 设备 均具有以下名称:
其中
XXXX
是设备地址的最后 4 位数字。这意味着您可以使用以下内容连接到其中的任何:这正是我在我的应用程序中所做的,但尚未找到任何更可靠/一致的方法来执行此操作。一般来说,如果您感兴趣的设备中的名称比上面的示例更复杂,您应该能够使用常规模式。
Devices of the same kind/functionality and/or brand will usually have a similar name. For example, all RN-41 devices from Roving Networks have the following name:
where
XXXX
is the last 4 digits of the device's address. That means you can use the following to connect to any of them:This is exactly what I do in my app and haven't found any more reliable/consistent way to do it. As a generalization, you should be able to use a regular pattern if the name in the devices you are interested in is any more complex than the example above.
您可以使用
myDevice.getAddress()
获取蓝牙设备地址并进行比较,它始终是唯一的(与名称不同)You can use
myDevice.getAddress()
to get the bluetooth device address and compare, it will always be unique (unlike name)您还可以使用 BluetoothDevice.getBluetoothClass() 来缩小可能相关的设备范围。
BluetoothClass.getMajorDeviceClass() 会大致告诉您它是什么类型的设备 - 电话、计算机、音频或视频设备或其他设备。
BluetoothClass.hasService() 进一步指定了设备的一些功能。
在每个主要类别中,定义了一些次要类别 - 它是什么类型的计算机/音频视频设备/电话/健康设备等。
此外,在最新版本的 Android 平台(API 级别 15+)上,您可以查询设备的服务记录,而无需连接到该设备。请参阅BluetoothDevice.fetchUuidsWithSdp() 和BluetoothDevice.getUuids()。
You can also use BluetoothDevice.getBluetoothClass() for at narrowing down which devices might be relevant.
BluetoothClass.getMajorDeviceClass() will tell you roughly what kind of device it is - a phone, a computer, an audio or video device, or whatever.
BluetoothClass.hasService() further specifies some capabilities of the device.
Within each of the major classes, some minor classes are defined - what kind of computer / audio-video device / phone / health equipment etc. it is.
Also, on recent versions of the Android platform (API level 15+), you can query for the service records of a device, without having to connect to it. See BluetoothDevice.fetchUuidsWithSdp() and BluetoothDevice.getUuids().