【问题标题】:Heart Rate measuring using Xiaomi MiBand and BLE使用小米手环和BLE测量心率
【发布时间】:2016-03-30 14:28:48
【问题描述】:

我正在尝试实现简单的 sdk,以便与健身追踪器小米 Mi Band 一起使用。目前我可以跟踪步数、启动振动、处理传感器触摸,但我在心率测量方面遇到了问题。我的sdk 基于https://github.com/pangliang/miband-sdk-android。为了测量心率,我需要将描述符写入适当的特性,以便在该特性的值发生更改时启用处理回调,然后将适当的数据写入心率控制点特性以直接启动心率测量过程。问题是在启动此过程的数据成功写入特征后,心率测量过程未启动(当小米手环开始测量心率时,底部传感器闪烁绿色)。这可能是由于健身追踪器的新固件(固件版本:4.15.12.10;心率版本:1.3.74.64)或我的代码存在一些缺陷:

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

也许该协议已被弃用,有人可以与我分享一个新协议。我会很高兴)谢谢。

【问题讨论】:

  • 亚历克斯,你能告诉我你是否使用加速度计监听来测量步数跟踪吗?还是有什么其他的魔法感应器?
  • Krystian,你可以查看我的仓库,那里有一个应用程序实现了实时步数跟踪和心率测量github.com/AlexanderHryk/MiFood

标签: java android bluetooth-lowenergy


【解决方案1】:

我解决了这个问题!在调用setNotifyListener跟踪脉搏值变化之前,您必须将用户信息(年龄、体重)写入适当的特征,因为没有这个小米手环不会开始测量。

【讨论】:

  • 我正在尝试做同样的事情,但没有成功。我可以读取之前存储的心率值,但无法触发测量。您知道用户信息特征的规范吗?您愿意分享这些信息吗?
  • 别担心,我找到了用户信息特征 [这里] (github.com/pangliang/miband-sdk-android/blob/master/miband-sdk/…)
  • 小米手环4尝过了吗?
【解决方案2】:

我正在与 BLE A&D 合作,以获取我使用的信息:

查找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

BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {

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

使用 BleGatt 示例 https://github.com/googlesamples/android-BluetoothLeGatt

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多