针对葡萄糖的Android BLE通知

问题描述 投票:1回答:1

我厌倦了使用Android BLE SDK与我的Glucose设备进行通信。我需要UUID 2a18和2a34的setCharacteristicNotification。我指的是Android官方SDK如下:

http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#notification

BluetoothGattCharacteristic charGM = 
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
    .getCharacteristic(UUID.fromString(BleUuid.CHAR_GLUCOSE_MEASUREMENT_STRING));
mConnGatt.setCharacteristicNotification(charGM, enabled);
BluetoothGattDescriptor descGM = charGM.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descGM.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descGM);

但它甚至无法进入onCharacteristicChanged回调。

我的onCharacteristicChanged如下:

        public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(),"onCharacteristicChanged",Toast.LENGTH_LONG).show();
                setProgressBarIndeterminateVisibility(false);
            };
        });
    }

如果我按如下方式设置电池电量通知,则可行。

BluetoothGattCharacteristic charBarrery = 
    mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_BATTERY))
        .getCharacteristic(UUID.fromString(BleUuid.CHAR_BATTERY_LEVEL_STRING));
mConnGatt.setCharacteristicNotification(charBarrery, enabled);
BluetoothGattDescriptor descBarrery = charBarrery.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descBarrery.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descBarrery);

我不知道电池和葡萄糖通知有什么不同。

如果有人知道我该怎么做,那么请帮助我。

额外:

当我用电池服务时,我的logcat如下:

07-29 10:28:17.924: D/BluetoothGatt(947): setCharacteristicNotification() - uuid: 00002a19-0000-1000-8000-00805f9b34fb enable: true
07-29 10:28:17.924: D/BluetoothGatt(947): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
07-29 10:28:18.484: D/BluetoothGatt(947): onDescriptorWrite() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
07-29 10:28:18.604: D/BluetoothGatt(947): onNotify() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb

但是当我使用Glucose时,我的logcat会丢失onNotify(),如下所示:

07-29 10:31:23.729: D/BluetoothGatt(1763): setCharacteristicNotification() - uuid: 00002a18-0000-1000-8000-00805f9b34fb enable: true
07-29 10:31:23.729: D/BluetoothGatt(1763): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
07-29 10:31:24.324: D/BluetoothGatt(1763): onDescriptorWrite() - Device=B4:AB:2C:06:9E:F4 UUID=00002a18-0000-1000-8000-00805f9b34fb

我不知道为什么logcat失去onNotify()...

额外的(8/4):

感谢您的回复 !我尝试在记录访问控制点特征上启用指示但失败了..我的过程如下:

  1. 启用葡萄糖测量特征和葡萄糖测量环境特征的通知并启用记录访问控制点特征的指示 public void onServicesDiscovered(BluetoothGatt gatt, int status) { for (BluetoothGattService service : gatt.getServices()) { if ((service == null) || (service.getUuid() == null)) { continue; } if (BleUuid.SERVICE_GLUCOSE.equalsIgnoreCase(service .getUuid().toString())) { BluetoothGattCharacteristic charGM = mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE)) .getCharacteristic(UUID.fromString(BleUuid.CHAR_GLUCOSE_MEASUREMENT_STRING)); mConnGatt.setCharacteristicNotification(charGM, enabled); BluetoothGattDescriptor descGM = charGM.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING)); descGM.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mConnGatt.writeDescriptor(descGM); BluetoothGattCharacteristic charGMC = mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE)) .getCharacteristic(UUID.fromString(BleUuid.CHAR_GLUCOSE_MEASUREMENT_CONTEXT_STRING)); mConnGatt.setCharacteristicNotification(charGMC, enabled); BluetoothGattDescriptor descGMC = charGMC.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING)); descGMC.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mConnGatt.writeDescriptor(descGMC); BluetoothGattCharacteristic charRACP = mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE)) .getCharacteristic(UUID.fromString(BleUuid.CHAR_RECORD_ACCESS_CONTROL_POINT_STRING)); mConnGatt.setCharacteristicNotification(charRACP, enabled); BluetoothGattDescriptor descRACP = charRACP.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING)); descRACP.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mConnGatt.writeDescriptor(descRACP); BluetoothGattCharacteristic charBarrery = mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_BATTERY)) .getCharacteristic(UUID.fromString(BleUuid.CHAR_BATTERY_LEVEL_STRING)); mConnGatt.setCharacteristicNotification(charBarrery, enabled); BluetoothGattDescriptor descBarrery = charBarrery.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING)); descBarrery.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mConnGatt.writeDescriptor(descBarrery); runOnUiThread(new Runnable() { public void run() { btnUpdateData.setEnabled(true); }; }); } } };
  2. 发送0x0101以记录访问控制点 case R.id.btnUpdateData: try{ //***SEND 0x0101 TO RECORD ACCESS CONTROL POINT BluetoothGattCharacteristic writeRACPchar = mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE)) .getCharacteristic(UUID.fromString(BleUuid.CHAR_RECORD_ACCESS_CONTROL_POINT_STRING)); byte[] data = new byte[1]; data[0] = (byte)0x0101; writeRACPchar.setValue(data); mConnGatt.writeCharacteristic(writeRACPchar); }catch(Exception e){ e.printStackTrace(); } break;
  3. 我的回调函数 @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { }; @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); }

&

    private void broadcastUpdate(final String action,
        final BluetoothGattCharacteristic characteristic) {
        final Intent intent = new Intent(action);
        if (BleUuid.CHAR_SERIAL_NUMBEAR_STRING
                .equalsIgnoreCase(characteristic.getUuid().toString())) {
            displayResult(characteristic.getStringValue(0));
        }else if(BleUuid.CHAR_MANUFACTURER_NAME_STRING
                .equalsIgnoreCase(characteristic.getUuid().toString())){
            displayResult(characteristic.getStringValue(0));
        } else if(BleUuid.CHAR_BATTERY_LEVEL_STRING
                .equalsIgnoreCase(characteristic.getUuid().toString())){
            final byte[] data = characteristic.getValue();
            String dataStr = "";
            dataStr = String.format("%02X", data[0]);
            int a = Integer.parseInt(dataStr, 16);
            String result = "battery level: " + Integer.toString(a)+ "%";
            displayResult(result);
        } else {
            // For all other profiles, writes the data formatted in HEX.
            final byte[] data = characteristic.getValue();
            if (data != null && data.length > 0) {
                final StringBuilder stringBuilder = new StringBuilder(data.length);
                for(byte byteChar : data)
                    stringBuilder.append(String.format("%02X ", byteChar));
                displayResult(stringBuilder.toString());
            }
        }
}

&

    private void displayResult(String result){
    adUpdateData.add(result);
    runOnUiThread(new Runnable() {
        public void run() {
            lvUpdateData.setAdapter(adUpdateData);
        };
    });
}

我试图理解“GLS_SPEC”pdf ...我使用了Nordic Semiconductor的Glucose Service样本应用程序,它可以工作。我试着学习如何达到这个功能。

我注意到一些日志就像“unregisterIRListener()被称为”在LogCat上显示,但我不确定是否关于我的问题......

谢谢阅读。

android notifications bluetooth-lowenergy android-notifications
1个回答
4
投票

葡萄糖和电池服务存在逻辑差异。当您通常启用电池电量通知时(取决于实施),设备会立即向您发送电池电量。这就是你在那里获得通知的原因。当然,在某些实现中,它仅在值更改时或通过其他规则通知您,但在您的情况下,它看起来像这样。

葡萄糖服务有所不同。在葡萄糖服务(https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.glucose.xml),您有3个强制性特征。我们在这里跳过葡萄糖特征。其中之一是葡萄糖测量,您可以获得葡萄糖读数通知。您启用这些通知的实现是正确的,它将起作用。但是为了获得通知,您必须使用记录访问控制点特性来请求它们。它允许您获取所有葡萄糖读数,只有最新的,只有第一个,从设备中删除已保存的读数等。例如,当您:

  1. 启用葡萄糖测量特征通知(就像你一样)
  2. 启用记录访问控制点特征的指示
  3. 发送f.e. 0x0101 =报告存储的记录|所有记录

您应该获得有关Glucose Measurement char的N次通知。然后是RACP char的指示。值:0x06000101 =“报告存储记录”的响应|成功。如果葡萄糖装置上没有保存读数,则N可以是0。如果您正在使用Nordic Semiconductor的葡萄糖服务样品应用程序,您可以尝试按下按钮0(?)在电路板上生成新结果(最多20个)并再次请求。

阅读GLS文档:https://www.bluetooth.org/en-us/specification/adopted-specifications - > GLS - > PDF,了解有关葡萄糖服务和记录访问控制点格式的更多信息。

编辑

您正在编写记录访问控制点值错误。这是工作代码:

case R.id.btnUpdateData:
    try{
        //***SEND 0x0101 TO RECORD ACCESS CONTROL POINT   
        BluetoothGattCharacteristic writeRACPchar = 
                mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
                    .getCharacteristic(UUID.fromString(BleUuid.CHAR_RECORD_ACCESS_CONTROL_POINT_STRING));
        byte[] data = new byte[2];
        data[0] = 0x01; // Report Stored records
        data[1] = 0x01; // All records
        writeRACPchar.setValue(data);

        // or:
        // byte[] data = new byte[2];
        // writeRACPchar.setValue(data);
        // writeRACPchar.setIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1, 0); // Report Stored records
        // writeRACPchar.setIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1, 1); // Read all (offset 1)
        mConnGatt.writeCharacteristic(writeRACPchar);
    }catch(Exception e){
        e.printStackTrace();
    }
    break;

你必须写2个字节,每个字节是8个buts,可以写成2位HEX,f.e。 0xAB r 0x01。值0x0101有2个字节,您可能没有

© www.soinside.com 2019 - 2024. All rights reserved.