蓝牙LE设备 - 如何在移动应用中识别某种类型的设备?

时间:2015-04-22 14:07:45

标签: bluetooth-lowenergy android-bluetooth ios-bluetooth

我正在学习使用蓝牙LE设备进行编程并编写一个简单的移动应用程序。这是我的入门级问题:

假设我只想连接到某种类型的蓝牙LE设备(如血压设备),但是当我进行扫描时,如果范围内存在其他蓝牙LE设备,则可能会返回多个结果。所以我可能得到以下结果:

设备1 RSSI,设备1名称,设备1地址;

设备2 RSSI,设备2名称,设备2地址 ...

如何告诉代码选择我想要的设备类型(在这种情况下,是血压设备)?设备地址是否由产品供应商分配,它们是否足够独特并遵循我可用于识别此类设备的方案?如果没有,我还有什么其他选项让应用程序自动识别某种类型的蓝牙设备?

3 个答案:

答案 0 :(得分:2)

如果你想拿起特定的设备意味着你必须在displayGattServices中提及。 例如: - 使用您提到过的心率传感器设备

 if (SampleGattAttributes.lookup(uuid, unknownCharaString)
                        .contains("Heart")) {
                    hrt_rate_char = gattCharacteristic;
                }

有关详细信息,请参阅displayGattServices方法:

private void displayGattServices(List<BluetoothGattService> gattServices) {
        if (gattServices == null)
            return;
        String uuid = null;
        String unknownServiceString = getResources().getString(
                R.string.unknown_service);

        String unknownCharaString = getResources().getString(
                R.string.unknown_characteristic);

        ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();

        ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData = new ArrayList<ArrayList<HashMap<String, String>>>();

        mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

        for (BluetoothGattService gattService : gattServices) {
            HashMap<String, String> currentServiceData = new HashMap<String, String>();

            uuid = gattService.getUuid().toString();
            currentServiceData.put(LIST_NAME,
                    SampleGattAttributes.lookup(uuid, unknownServiceString));

            currentServiceData.put(LIST_UUID, uuid);
            gattServiceData.add(currentServiceData);

            ArrayList<HashMap<String, String>> gattCharacteristicGroupData = new ArrayList<HashMap<String, String>>();
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService
                    .getCharacteristics();

            ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<BluetoothGattCharacteristic>();

            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                charas.add(gattCharacteristic);

                HashMap<String, String> currentCharaData = new HashMap<String, String>();

                uuid = gattCharacteristic.getUuid().toString();
                if (SampleGattAttributes.lookup(uuid, unknownCharaString)
                        .contains("Heart")) {
                    hrt_rate_char = gattCharacteristic;
                }
                currentCharaData.put(LIST_NAME,
                        SampleGattAttributes.lookup(uuid, unknownCharaString));

                currentCharaData.put(LIST_UUID, uuid);
                gattCharacteristicGroupData.add(currentCharaData);

            }
            mGattCharacteristics.add(charas);

            gattCharacteristicData.add(gattCharacteristicGroupData);

        }
    }

答案 1 :(得分:1)

以上代码不起作用。您必须扫描设备,连接到设备并发现服务。心率设备具有特定的服务特征。这是一个链接: https://developer.bluetooth.org/TechnologyOverview/Pages/HRP.aspx

查看我的答案的这个链接,以证明心率设备: https://stackoverflow.com/a/29548205/862382

答案 2 :(得分:1)

Suppose if you want your app to connect only to Hear Rate monitoring devices, and if you have the liberty to connect and check, you should use the standard Heart Rate service implementation. But, suppose if you want to get it done without connecting to a BLE device, Appearance field in the advertisement packet should help you, provided that BLE Server developers have taken proper care to set the device appearance in the GAP service.

Appearance values query.fromLocalDatastore() and func enumFromString(string:String) -> MyEnum? { switch string { case "One" : MyEnum(rawValue:1) case "Two" : MyEnum(rawValue:2) case "Three" : MyEnum(rawValue:3) default : return nil } } are for 832 and 833.

That said, be assured that you will also have the capability to connect to custom devices that implement Heart Rate Service as a supplementary service with their primary application, only if the custom service has the GAP characteristics Appearance set to 833 or 832.