2016-03-30 3 views
10

Ich versuche, einfache SDK für die Arbeit mit Fitness-Tracker Xiaomi Mi Band zu implementieren. Derzeit kann ich Schritte verfolgen, die Vibration aktivieren, den Sensor berühren, aber ich habe ein Problem mit der Herzfrequenzmessung. Mein SDK basiert auf. Um die Herzfrequenz zu messen, muss ich den Deskriptor in das entsprechende Merkmal schreiben, um die Behandlung des Rückrufs zu ermöglichen, wenn der Wert dieses Merkmals geändert wurde, und dann entsprechende Daten in die Herzfrequenzkontrollpunktcharakteristik schreiben, um den Herzfrequenzmessprozess direkt zu starten. Das Problem ist, dass der Herzfrequenz-Messvorgang nicht gestartet wurde, nachdem die Daten zum Initiieren dieses Prozesses erfolgreich geschrieben wurden (wenn das Mi-Band beginnt, die Herzfrequenz zu messen, blinkt der untere Sensor grün). Dies kann durch eine neue Firmware von Fitness-Tracker verursacht werden (: 4.15.12.10, Herzfrequenz-Version: Firmware-Version 1.3.74.64) oder gibt es einige Mängel in meinem Code:Herzfrequenzmessung mit Xiaomi MiBand und BLE

/-------- MiBandProfile --------/ 
public static final UUID UUID_SERVICE_HEARTRATE = UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb"); 
public static final UUID UUID_NOTIFICATION_HEARTRATE = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb"); 
public static final UUID UUID_CHAR_HEARTRATE = UUID.fromString("00002a39-0000-1000-8000-00805f9b34fb"); 
public static final UUID UUID_DESCRIPTOR_UPDATE_NOTIFICATION = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); 

/-------- MiBandProtocol --------/ 
public static final byte[] START_HEART_RATE_SCAN = {21, 1, 1}; 

/-------- BleProvider --------/ 
public class BleProvider extends BluetoothGattCallback { 

public interface NotifyListener { 
    void onNotify(byte[] data); 
} 

private HashMap<UUID, NotifyListener> mNotifyListeners = new HashMap<UUID, NotifyListener>(); 
. 
. 
. 
public void setNotifyListener(UUID serviceUUID, UUID charaUUID, NotifyListener listener) { 
     //enable chara notofication 
     if (this.mGatt == null || !this.mIsServicesDiscovered) { 
      if (DBG) Log.d(Debug.TAG, "Device is not connected or services are not discovered"); 
      return; 
     } 

     BluetoothGattCharacteristic chara = this.mGatt.getService(serviceUUID).getCharacteristic(charaUUID); 

     if (DBG) Log.d(Debug.TAG, "setCharacteristicNotification: " + this.mGatt.setCharacteristicNotification(chara, true)); 
     BluetoothGattDescriptor descriptor = chara.getDescriptor(MiBandProfile.UUID_DESCRIPTOR_UPDATE_NOTIFICATION); 
     if (DBG) Log.d(Debug.TAG, "setValue: " + descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)); 
     if (DBG) Log.d(Debug.TAG, "writeDescriptor: " + this.mGatt.writeDescriptor(descriptor)); 
     this.mNotifyListeners.put(charaUUID, listener); 
} 

@Override 
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { 
     //this method must be called when the characteristic value was changed but nothing happened :(
     super.onCharacteristicChanged(gatt, characteristic); 

     if (this.mNotifyListeners.containsKey(characteristic.getUuid())) { 
      this.mNotifyListeners.get(characteristic.getUuid()).onNotify(characteristic.getValue()); 
     } 
} 
} //end BleProvider 

. 
. 
. 

setNotifyListener(MiBandProfile.UUID_SERVICE_HEARTRATE, MiBandProfile.UUID_NOTIFICATION_HEARTRATE, new BleProvider.NotifyListener(){...}); 
//waiting few seconds 
writeCharacteristic(MiBandProfile.UUID_SERVICE_HEARTRATE, MiBandProfile.UUID_CHAR_HEARTRATE, MiBandProtocol.START_HEART_RATE_SCAN); 

Vielleicht ist das Protokoll ist veraltet und es gibt Leute, die mir ein neues Protokoll mitteilen können. Ich werde sehr froh sein) Danke.

+0

Alex, könnten Sie mir sagen, ob Schritte Tracking mit Beschleunigungsmesser Sie messen? Oder gibt es einen anderen magischen Sensor? – Krystian

+0

Krystian, können Sie meine Repo überprüfen, wo es eine App mit implementierten Echtzeit-Tracking-und Herzfrequenzmessung https://github.com/AlexanderHryk/MiFood –

Antwort

4

ich das Problem gelöst! Bevor Sie setNotifyListener aufrufen, um die Änderung des Pulswerts zu verfolgen, müssen Sie die Benutzerinformation (Alter, Gewicht) in das entsprechende Merkmal schreiben, da ohne dieses Mi-Band die Messung nicht startet.

+0

Ich versuche, das gleiche zu tun, ohne Erfolg. Ich kann vorher gespeicherte Herzfrequenzwerte lesen, kann aber die Messung nicht auslösen. Kennen Sie die Spezifikation der Benutzerinformationen? Würde es Ihnen etwas ausmachen, diese Informationen zu teilen? – javsmo

+0

Keine Sorge, ich habe die Benutzerinformationen gefunden [hier] (https://github.com/pangliang/miband-sdk-android/blob/master/miband-sdk/src/main/java/com/zhaoxiaodan/ miband/modell/UserInfo.java) – javsmo

1

Im Umgang mit BLE A & D, die iformation i verwenden, um obatin:

Für UUID

@SuppressLint("NewApi") 
private void displayGattServicesService(List<BluetoothGattService> gattServices) { 
    // salida.append("Buscando servicio"); 
    if (gattServices == null) 
     return; 
    for (BluetoothGattService gattService : gattServices) { 

     String uuid = gattService.getUuid().toString(); 

     List<BluetoothGattCharacteristic> gattCharacteristics = gattService 
       .getCharacteristics(); 
     if (uuid.equals("00001810-0000-1000-8000-00805f9b34fb")) { 
      // salida.append("Ecnontro"); 
      for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { 
       String uuid1 = gattCharacteristic.getUuid().toString(); 
       if (uuid1.equals("00002a35-0000-1000-8000-00805f9b34fb")) { 
        //  salida.append("Envio caracterisitca"); 
        mensaje(getResources().getString(R.string.Espere_res)); 
        mBluetoothLeService.setCharacteristicNotification(
          gattCharacteristic, true); 
       } 
      } 
     } 
    } 
} 

UUID So stellen

BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { 

      // Show all the supported services and characteristics on the user interface. 
      displayGattServicesService(mBluetoothLeService.getSupportedGattServices()); 

Verwenden BleGatt Beispiel https://github.com/googlesamples/android-BluetoothLeGatt