Android 关于葡萄糖的可撤销通知

Android 关于葡萄糖的可撤销通知,android,notifications,bluetooth-lowenergy,android-notifications,Android,Notifications,Bluetooth Lowenergy,Android Notifications,我厌倦了使用Android BLE SDK与我的葡萄糖设备通信。 我需要UUID 2a18和2a34的setCharacteristicNotification。我对Android官方SDK的参考如下: public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)

我厌倦了使用Android BLE SDK与我的葡萄糖设备通信。 我需要UUID 2a18和2a34的setCharacteristicNotification。我对Android官方SDK的参考如下:

        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);
            };
        });
    }
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

但它甚至不能进入onCharacteristicChanged回调

我的OnCharacteristic更改如下:

        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);
            };
        });
    }
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
如果我按如下方式设置电池电量通知,它会工作

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);
我不知道电池和葡萄糖有什么不同

如果有人知道我该做什么,请帮助我

附加:

当我使用电池服务时,我的日志如下:

        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);
            };
        });
    }
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
但当我使用葡萄糖时,我的日志会丢失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):

谢谢你的回复! 我尝试在记录访问控制点特性上启用指示,但失败。。 我的过程如下:

        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);
            };
        });
    }
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
  • 启用有关葡萄糖测量特征和葡萄糖测量上下文特征的通知 & 启用记录访问控制点特性的指示

        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);
                    };
                });
            }
        }
    };
    
  • 发送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;
    
  • 我的回调函数

    @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);
    }
    
  • &

    &

    我正在努力理解“GLS_规范”pdf。。。 我使用了Nordic Semiconductor的葡萄糖服务示例应用程序,它可以正常工作。 我试图学习如何达到这个功能

    我注意到LogCat上显示了一些类似“UnregisterListener()被调用”的日志,但我不确定我的问题是否


    感谢阅读。

    葡萄糖和电池服务在逻辑上存在差异。当您启用电池电量通知时,通常(取决于实现),设备会立即向您发送电池电量。这就是为什么你要去那里。当然,在某些实现中,它仅在值更改时通知您,或者使用其他规则时通知您,但在您的情况下,它的行为类似于此

    葡萄糖服务的工作原理不同。在Glucose服务()中,您有3个必需的特征。让我们跳过这里的葡萄糖功能。 其中之一是血糖测量,在这里你可以得到血糖读数通知。您启用这些通知的实现是正确的,它将起作用。但为了获得通知,您必须使用记录访问控制点特性请求它们。它允许您获取所有葡萄糖读数,仅获取最新的、仅获取第一次的、从设备中删除保存的读数等。例如,当您:

  • 启用有关葡萄糖测量特性的通知(就像您所做的那样)
  • 启用记录访问控制点特性的指示
  • 发送f.e.0x0101=报告存储的记录|所有记录
  • 你应该得到N个关于葡萄糖测量的通知。后跟RACP char上的指示。值为:0x06000101=对“报告存储记录”的响应|成功。 如果葡萄糖装置上未保存读数,则N可能为0。如果您正在使用Nordic Semiconductor的葡萄糖服务样本应用程序,您可以尝试按按钮0(?)在电路板上生成新结果(最多20个),然后再次请求

    阅读GLS文档:->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个,但可以写入2位十六进制,f.e.0xAB r 0x01。值0x0101有2个字节,您可能不知道葡萄糖和电池服务在逻辑上存在差异。当您启用电池电量通知时,通常(取决于实现),设备会立即向您发送电池电量。这就是为什么你要去那里。当然,在某些实现中,它仅在值更改时通知您,或者使用其他规则时通知您,但在您的情况下,它的行为类似于此

    葡萄糖服务的工作原理不同。在Glucose服务()中,您有3个必需的特征。让我们跳过这里的葡萄糖功能。 其中之一是血糖测量,在这里你可以得到血糖读数通知。您启用这些通知的实现是正确的,它将起作用。但为了获得通知,您必须使用记录访问控制点特性请求它们。它允许您获取所有葡萄糖读数,仅获取最新的、仅获取第一次的、从设备中删除保存的读数等。例如,当您:

  • 启用有关葡萄糖测量特性的通知(就像您所做的那样)
  • 启用记录访问控制点特性的指示
  • 发送f.e.0x0101=报告存储的记录|所有记录
  • 你应该得到N个关于葡萄糖测量的通知。后跟RACP char上的指示。值为:0x06000101=对“报告存储记录”的响应|成功。 如果葡萄糖装置上未保存读数,则N可能为0。如果您正在使用Nordic Semiconductor的葡萄糖服务样本应用程序,您可以尝试按按钮0(?)在电路板上生成新结果(最多20个),然后再次请求

    阅读GLS文档:->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个,但可以写入2位十六进制,f.e.0xAB r 0x01。值0x0101有2个字节,您可能无法

    使用哪种葡萄糖设备?使用哪种葡萄糖设备?Hello@philips77,我也在尝试实现同样的功能。你能看看我的问题并帮我继续吗。这里是链接:Hello@philips77,我也在尝试实现同样的东西。你能看看我的问题并帮我继续吗。以下是链接: