I want to get the Heart Rate through BluetoothLe from my Mi Band 2. I tried to follow the example from Getting Started with Bluetooth Low Energy but I didn't succeed to write the value from Heart Rate. I think I miss something but I cannot realize what. It's the first time I am working with bluetooth and smart band. I wish you could help me. I do not know if for starting the Heart Rate sensor should I use as {0x01} byte.

So following the example I downloaded the BluetoothLeDemo app and I got from there BleWrapper BleDefinedUUIDs and BleNamesResolver. I have 2 buttons Scan and stop. I created a BleWrapper, called mBleWrapper to use it for starting scanning and stop scanning.

mBleWrapper = new BleWrapper(this, new BleWrapperUiCallbacks.Null(){


            @Override
            public void uiDeviceConnected(BluetoothGatt gatt, BluetoothDevice device) {
                super.uiDeviceConnected(gatt, device);
            }

            @Override
            public void uiDeviceDisconnected(BluetoothGatt gatt, BluetoothDevice device) {
                super.uiDeviceDisconnected(gatt, device);
            }

            @Override
            public void uiAvailableServices(BluetoothGatt gatt, BluetoothDevice device, List<BluetoothGattService> services) {
                super.uiAvailableServices(gatt, device, services);

                BluetoothGattCharacteristic c=null;

                for(BluetoothGattService service : services) {
                    String serviceName = BleNamesResolver.resolveUuid(service.getUuid().toString());
                    Log.i("SERVIDE", serviceName);
                    }
                }
c=gatt.getService(BleDefinedUUIDs.Service.HEART_RATE).getCharacteristic(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT);
                mBleWrapper.writeDataToCharacteristic(c, new byte[]{0x01});
                mState = mSensorState.ACC_ENABLE;

            }

            @Override
            public void uiCharacteristicForService(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, List<BluetoothGattCharacteristic> chars) {
                super.uiCharacteristicForService(gatt, device, service, chars);

            }

            @Override
            public void uiCharacteristicsDetails(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic characteristic) {
                super.uiCharacteristicsDetails(gatt, device, service, characteristic);
            }

            @Override
            public void uiNewValueForCharacteristic(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic ch, String strValue, int intValue, byte[] rawValue, String timestamp) {
                super.uiNewValueForCharacteristic(gatt, device, service, ch, strValue, intValue, rawValue, timestamp);
                switch (mState) {
                    case ACC_READ:
                        Log.i("READ", "heart rate dta");
                }

                    Log.i("Value", "Val" + intValue);

            }

            @Override
            public void uiGotNotification(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic characteristic) {
                super.uiGotNotification(gatt, device, service, characteristic);
                String ch = BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString());

                Log.d("AAA",  "uiGotNotification: " + ch);
            }

            @Override
            public void uiSuccessfulWrite(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic ch, String description) {
                super.uiSuccessfulWrite(gatt, device, service, ch, description);
                BluetoothGattCharacteristic c;
                switch (mState) {
                    case ACC_ENABLE:
                        Log.i("ENABLED", "Heart Rate enabled");
                        c = gatt.getService(BleDefinedUUIDs.Service.HEART_RATE).getCharacteristic(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT);
                        mBleWrapper.requestCharacteristicValue(c);
                        mState = mSensorState.ACC_READ;
                        break;
                    case ACC_READ:
                        Log.i("WRITE", "SUCCESSfule write");
                        break;
                }
            }

            @Override
            public void uiFailedWrite(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic ch, String description) {
                super.uiFailedWrite(gatt, device, service, ch, description);
                switch (mState) {
                    case ACC_ENABLE:
                        Log.i("FAILED", "Feailed to enbale Heart Rate");
                }
            }

            @Override
            public void uiNewRssiAvailable(BluetoothGatt gatt, BluetoothDevice device, int rssi) {
                super.uiNewRssiAvailable(gatt, device, rssi);
            }

            @Override
            public void uiDeviceFound(BluetoothDevice device, int rssi, byte[] record) {
                super.uiDeviceFound(device, rssi, record);
                String msg = "uiDeviceFound: "+device.getAddress()+","+device.getName();
                if(devicesList.contains(device)==false) {
                    Log.i("Devicce", "deviceFound: " + msg);
                    devicesList.add(device);
                    genericListAdapter.notifyDataSetChanged();
                }

            }
        });

I display all devices found with a ListView and onClick I want to connect to that item and write the Heart Rate in the Log.

 deviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             BluetoothGatt gatt;
             BluetoothGattCharacteristic c;
             connectDevice((BluetoothDevice) parent.getItemAtPosition(position));

             Log.i("Read", "Get Heart Rate");
             if(mBleWrapper.isConnected()==false){
                Log.i("not connected", "NOT CONNECTE");
                 return;
             }

             gatt = mBleWrapper.getGatt();
             c = gatt.getService(BleDefinedUUIDs.Service.HEART_RATE).getCharacteristic(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT);
             mBleWrapper.requestCharacteristicValue(c);
             mState=mSensorState.ACC_READ;


            }
        });

I also added at BleWrapper class onDescpritorWriter

 @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);

            String deviceName = gatt.getDevice().getName();
            String serviceName = BleNamesResolver.resolveServiceName(descriptor.getCharacteristic().getService().getUuid().toString().toLowerCase(Locale.getDefault()));
            String charName = BleNamesResolver.resolveCharacteristicName(descriptor.getCharacteristic().getUuid().toString().toLowerCase(Locale.getDefault()));
            String description = "Device: " + deviceName + " Service: " + serviceName + " Characteristic: " + charName;

            if(status == BluetoothGatt.GATT_SUCCESS) {
                mUiCallback.uiSuccessfulWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, descriptor.getCharacteristic(), description);
            }
            else {
                mUiCallback.uiFailedWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, descriptor.getCharacteristic(), description + " STATUS = " + status);
            }

        }

And for UUIDS for service I used

UUID HEART_RATE = UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb");

and for Charachteristics

UUID HEART_RATE_MEASUREMENT   = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb");

In other examples I saw that they also used the UUIDS for descriptor, but me not. I know that is a long post and question, but please I really need some help.

1 Answers

0
Yuan-han Tu On

There are something you can check around.

Obviously, you have successfully discovered devices around. According to this(Bluetooth official document) heart rate service (UUID:180d) need set notify to TRUE first. Something like:

  1. Use UUID:00002902-0000-1000-8000-00805f9b34fb to get the descriptor of the characteristic.
 BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptor(Client_Characteristic_Configuration);
  1. Set the feature (Notify) of descriptor to TRUE:
 descriptor.setValue((BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE));
  1. Write the value to the BLE device (using Gatt object from the callback function)
gatt.writeDescriptor(descriptor);

After above, you may get the data in override function:

public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)

By this way:

BluetoothGattCharacteristic Char = gatt.getService(FORA_SERVICE_UUID).getCharacteristic(The uuid you want to connect);

byte[] data = Char.getValue();

And maybe you can show the example link. It can be more clear what you had used.