简体   繁体   English

Android 上的蓝牙 - 如何连接到正确的蓝牙设备?

[英]Bluetooth on Android - how to connect to the CORRECT bluetooth device?

I'm writing a program that speaks with an external accessory over rfcomm.我正在编写一个通过 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?我尝试查看BluetoothDevice的所有方法,但似乎没有任何帮助 - 名称真的是最好的方法吗? 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:我看到在某些地方人们说我应该使用 UUID,但它用于在我拥有它后打开设备的套接字:

            _socket = myDevice.createRfcommSocketToServiceRecord(MY_UUID);

is there a better way to do it?有更好的方法吗?

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:例如, Roving Networks 的所有 RN-41 设备都具有以下名称:

FireFly-XXXX

where XXXX is the last 4 digits of the device's address.其中XXXX是设备地址的最后 4 位数字。 That means you can use the following to connect to any of them:这意味着您可以使用以下内容连接到其中的任何一个:

        if (device.getName().startsWith("FireFly-")) {
            this.myDevice = device;
            break;
        }

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.作为概括,如果您感兴趣的设备中的名称比上面的示例更复杂,您应该能够使用常规模式。

You can also use BluetoothDevice.getBluetoothClass() for at narrowing down which devices might be relevant.您还可以使用 BluetoothDevice.getBluetoothClass() 来缩小可能相关的设备范围。

BluetoothClass.getMajorDeviceClass() will tell you roughly what kind of device it is - a phone, a computer, an audio or video device, or whatever. BluetoothClass.getMajorDeviceClass() 将大致告诉您它是什么类型的设备 - 电话、计算机、音频或视频设备,或任何其他设备。 BluetoothClass.hasService() further specifies some capabilities of the device. BluetoothClass.hasService() 进一步指定了设备的一些功能。

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.此外,在最新版本的Android平台(API级别15+)上,您无需连接即可查询设备的服务记录。 See BluetoothDevice.fetchUuidsWithSdp() and BluetoothDevice.getUuids().请参阅 BluetoothDevice.fetchUuidsWithSdp() 和 BluetoothDevice.getUuids()。

You can use myDevice.getAddress() to get the bluetooth device address and compare, it will always be unique (unlike name)您可以使用myDevice.getAddress()获取蓝牙设备地址并进行比较,它始终是唯一的(与名称不同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM