在 android BLE 上,我根据来自 BLE 设备的通知测量实时传感数据。默认情况下,我每秒获取 1 次通知数据。但是,我想在某个时间段内延迟从设备获取数据。下面是我的代码的屏幕截图。
setNotificationCallback(mTXCharacteristic)
.with(new TemperatureMeasurementDataCallback() {
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
PrintLog.i(" "+TemperatureMeasurementParser.parse(data) + " °C");
//Toast.makeText(getContext(), " "+TemperatureMeasurementParser.parse(data) + " °C", Toast.LENGTH_LONG).show();
//super.onDataReceived(device, data);
onTemperatureMeasurementReceived(device, Float.parseFloat(TemperatureMeasurementParser.parse(data)), TemperatureMeasurementCallback.UNIT_C, null, null);
}
@Override
public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
final float temperature, final int unit,
@Nullable final Calendar calendar,
@Nullable final Integer type) {
mCallbacks.onTemperatureMeasurementReceived(device, temperature, unit, calendar, type);
}
public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
final float temperature, final int unit,
@Nullable final Calendar calendar,
@Nullable final Integer type, @Nullable final Integer batteryLevel) {
mCallbacks.onTemperatureMeasurementReceived(device, temperature, unit, calendar, type,batteryLevel);
}
});
requestMtu(260).enqueue();
enableNotifications(mTXCharacteristic).enqueue();
//sleep(10000).enqueue();
最佳答案
正在从遥感设备发送通知。因此,如果要延迟或改变接收传感数据的时间间隔,需要修改传感器中的代码。这是正确可靠的方法。或者,您可以在 Android 应用程序上启动计时器(例如每 10 秒一次)。当您从远程设备收到通知时,不要显示它们。相反,将它们存储在全局变量中。然后,当 10 秒计时器到期时,打印这些变量并重新启动计时器。
希望对您有所帮助。
关于notifications - Android Ble 延迟通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55457158/