Android监听器(读取Mi波段传感器数据)

时间:2016-07-10 10:44:22

标签: java android sensor

我想从mi band获得真实的传感器(加速)数据。

我从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(数据)'

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

1 个答案:

答案 0 :(得分:0)

您将必须在onNotify回调中调用onDescriptorWrite,在此您需要验证描述符的UUID以及特征码的UUID 请参阅this