首页 > 解决方案 > 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 分钟,因为它正在被重置。

标签: androidbluetoothbluetooth-lowenergy

解决方案


推荐阅读