android - BLE 广告数据在一段时间后停止工作(溢出?)
问题描述
我有一个设备可以传输有关汽车速度、转速等的广告数据(无法连接)。我的 Android 应用每 10 秒左右读取一次该数据。该应用程序需要始终读取数据,即使在后台也是如此。它可以正常工作大约一个小时左右,但随后停止工作。ScanCallback 方法停止被调用,包括 onScanResult 和 onScanFailed。
开始扫描的方法:
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
scanner.startScan(null, settings, callback);
扫描回调:
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
//Update the UI with the latest info
final BluetoothDevice remoteDevice = result.getDevice();
final ScanRecord record = result.getScanRecord();
if(remoteDevice.getName() == null){
return;
}
else if(!remoteDevice.getName().equals("BT-ext")){
return;
}
//Post scan update to the UI thread
mHandler.post(new Runnable() {
@Override
public void run() {
updateScanResult(record);
}
});
}
@Override
public void onScanFailed(int errorCode) {
Log.w(TAG, "Error scanning devices: "+errorCode);
}
};
updateScanResult 会做一些 ui 工作,停止 ble 扫描,然后重新启动它。我认为蓝牙已经溢出,所以我重新开始扫描。然而,这并没有什么不同。
我已经在 Pixel 3 和三星 S9 上对此进行了测试,但它们都在一段时间后停止工作。我知道 Android 7.0 发生的蓝牙更改,但我认为这些限制不适用于此处。蓝牙没有运行超过 30 分钟,因为它正在被重置。
解决方案
推荐阅读
- python - 在 Pandas Column 中搜索每行值包含列表的字符串
- ruby-on-rails - Postgres 数组格式错误的数组字面量
- c# - 为 JsonSerializer.DeserializeAsync 动态创建一个类型?
- python - Tkinter 无法配置
- google-drive-shared-drive - 如何通过 gshell 导航到共享的谷歌驱动器?
- jmeter - Jmeter测试中每次迭代/循环中的增量变量
- git-for-windows - 无法初始化 git 存储库,因为 .git:没有这样的文件或目录
- javascript - 在我的 Javascript 应用程序上设置 Firebase 时,我可以使用 Parcel.JS 捆绑器吗?
- post - Python:运行带有错误消息的 POST 请求
- sql - 如何(取消)将多个指标转换为单个列?