title: How to get Heart Rate from Mi Band using Bluetooth Le
body: <p>I want to get the Heart Rate through BluetoothLe from my Mi Band 2. I tried to follow the example from <code>Getting Started with Bluetooth Low Energy</code> 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 <code>{0x01} byte</code>.</p>
  
  <p>So following the example I downloaded the BluetoothLeDemo app and I got from there <code>BleWrapper</code> <code>BleDefinedUUIDs</code> and <code>BleNamesResolver</code>. I have 2 buttons Scan and stop. I created a BleWrapper, called mBleWrapper to use it for starting scanning and stop scanning. </p>
  
  <pre class="lang-java prettyprint-override"><code>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&lt;BluetoothGattService&gt; 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&lt;BluetoothGattCharacteristic&gt; 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();
                  }
  
              }
          });
  </code></pre>
  
  <p>I display all devices found with a <code>ListView</code> and <code>onClick</code> I want to connect to that item and write the Heart Rate in the Log. </p>
  
  <pre class="lang-java prettyprint-override"><code> deviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; 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;
  
  
              }
          });
  </code></pre>
  
  <p>I also added at BleWrapper class <code>onDescpritorWriter</code></p>
  
  <pre class="lang-java prettyprint-override"><code> @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);
              }
  
          }
  </code></pre>
  
  <p>And for UUIDS for service I used</p>
  
  <pre><code>UUID HEART_RATE = UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb");
  </code></pre>
  
  <p>and for Charachteristics</p>
  
  <pre><code>UUID HEART_RATE_MEASUREMENT   = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb");
  </code></pre>
  
  <p>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.</p>