Android BLE notifications for Glucose

吃可爱长大的小学妹 提交于 2019-12-03 10:11:09

there is a logical difference in Glucose and Battery service. When you enable Battery Level notifications usually (depending on the implementation) a device sends you battery level immediately. That's why you get onNotify there. Of course in some implementations it notifies you only when the value changes, or with other rule, but in you case it looks like it behaves like this.

Glucose service works different. In the Glucose Service (https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.glucose.xml) you have 3 mandatory characteristics. Let's skip the Glucose Feature here. One of them is Glucose Measurement where you get glucose reading notifications. Your implementation to enable these notifications is correct, it will work. But in order to get notifications you have to request for them using Record Access Control Point characteristic. It allows you to get all glucose readings, only the latest, only the first, delete saved readings from the device etc. For example, when you:

  1. Enable notifications on Glucose Measurement characteristic (like you did)
  2. Enable indications on Record Access Control Point characteristic
  3. Send f.e. 0x0101 = Report stored records | All records

you should get N notifications on Glucose Measurement char. followed by an indication on RACP char. with value: 0x06000101 = Response for "Report stored records" | success. N may be 0 if no readings are saved on the glucose device. If you're using Nordic Semiconductor's Glucose Service sample application you may try pressing button 0(?) to generate a new result on the board (up to 20) and request again.

Read the GLS documentation: https://www.bluetooth.org/en-us/specification/adopted-specifications -> GLS -> PDF for more information about Glucose Service and Record Access Control Point format.

EDIT

You're writing the Record Access Control Point value wrong. Here's the working code:

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;

You have to write 2 bytes, each byte is 8 buts which may be written as 2-digit HEX, f.e. 0xAB r 0x01. The value 0x0101 has 2 bytes and you may not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!