安卓监听器(读取小米手环传感器数据)

4
我希望能够从小米手环中获取真实的传感器(加速度)数据。
我在Github上找到了很好的源代码,但是有一个问题。
请查看https://github.com/pangliang/miband-sdk-android和MainActivity.java。
else if (position == menuIndex++) {
                    Log.d(TAG, "setRealtimeStep");
                    miband.setRealtimeStepsNotifyListener(new RealtimeStepsNotifyListener() {
                        @Override
                        public void onNotify(int steps) {
                            Log.d(TAG, "RealtimeStepsNotifyListener:" + steps);
                        }
                    });

Miband.java

public void setSensorDataNotifyListener(final NotifyListener listener) {
    Log.d(TAG, "MiBand.java->setSensorDataNotifyListener");
    this.io.setNotifyListener(Profile.UUID_SERVICE_MILI, Profile.UUID_CHAR_SENSOR_DATA, new NotifyListener() {

        @Override
        public void onNotify(byte[] data) {
            Log.d(TAG, "MiBand.java->setSensorDataNotifyListener->onNotify");
            Log.d(TAG, data.toString());
            listener.onNotify(data);
        }
    });
}

BluetoothIO.java

public void setNotifyListener(UUID serviceUUID, UUID characteristicId, NotifyListener listener) {
    if (null == gatt) {
        Log.e(TAG, "connect to miband first");
        return;
    }

    BluetoothGattCharacteristic chara = gatt.getService(serviceUUID).getCharacteristic(characteristicId);
    if (chara == null) {
        Log.e(TAG, "characteristicId " + characteristicId.toString() + " not found in service " + serviceUUID.toString());
        return;
    }


    this.gatt.setCharacteristicNotification(chara, true);
    BluetoothGattDescriptor descriptor = chara.getDescriptor(Profile.UUID_DESCRIPTOR_UPDATE_NOTIFICATION);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    this.gatt.writeDescriptor(descriptor);
    this.notifyListeners.put(characteristicId, listener);

在这段代码中,我不知道哪一行可以获取传感器数据。 我也不知道应该在哪里添加 'OnNotify(data)'。

如果你有任何线索,请帮助我。 谢谢!

1个回答

0

在你的onDescriptorWrite回调函数中,你需要调用onNotify,同时验证描述符的UUID和特征值的UUID。详情请参考this


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接