微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android BLE 扫描注册超时

如何解决Android BLE 扫描注册超时

我正在开发一个连接到 BLE 设备以执行一些用户相关事务的应用程序。每次当应用程序需要连接到 BLE 设备时,我们都会使用以下逻辑启动扫描过程:

final List<ScanFilter> scanFilters = new ArrayList<>();

final ScanSettings scanSettings =
        new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
                .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
                .setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
                .build();

mBluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters,scanSettings,scanCallback);

这样做,有时 Android 的 BluetoothLeScanner 类会回调 onScanFailed()scanCallback错误代码为 2,表示无法启动对 BLE 设备的扫描。

在分析应用程序的日志后,我发现在调用 onScanFailed() 之后调用 startScan() 正好需要 2 秒。我发现在类 BluetoothLeScanner 内部,它等待来自本机 BLE 堆栈的回调正好 2 秒,以便它可以执行进一步的步骤来开始实际扫描。此逻辑是 BleScanCallbackWrapper 内的内部包装类 BluetoothLeScanner 的一部分。但在我的例子中,应用程序有时会在该等待点超时,因为它没有从 Android 的蓝牙管理器进程接收回调 onScannerRegistered()

我查看了互联网,发现这些 BLE 回调实际上是一个 IPC,并使用 Android Binder 在我们的应用程序进程中调用回调,反之亦然。我发现应用程序不应阻塞 BLE 回调,并且在绑定线程中运行时不应调用任何 BLE 方法

从高层次来看,回调事件似乎被延迟了(可能是因为我们的应用程序中的绑定线程被阻塞了)。以下是应用程序的一些日志:

03-10 16:11:45.457 13911 11611 I LoggingProviderImpl: [pool-30-f7] Starting scan for ble modules.
03-10 16:11:45.465 13911 11611 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:11:45.469 13911 27931 D BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=6 mScannerId=0
03-10 16:11:57.462 13911 11612 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:11:57.497 13911 11611 I LoggingProviderImpl: [pool-30-f7] Starting scan for ble modules.
03-10 16:11:57.499 13911 11611 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:11:59.504 13911 13911 I LoggingProviderImpl: [main] Scan Failed with error code: 2
03-10 16:11:59.573 13911 13911 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:12:03.495 13911 11611 I LoggingProviderImpl: [pool-30-f7] Starting scan for ble modules.
03-10 16:12:03.504 13911 11611 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:12:05.507 13911 13911 I LoggingProviderImpl: [main] Scan Failed with error code: 2
03-10 16:12:05.574 13911 13911 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:12:13.891 13911 11611 I LoggingProviderImpl: [pool-30-f7] Starting scan for ble modules.
03-10 16:12:13.900 13911 11611 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:12:15.904 13911 13911 I LoggingProviderImpl: [main] Scan Failed with error code: 2
03-10 16:12:15.966 13911 13911 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:12:20.910 13911 11611 I LoggingProviderImpl: [pool-30-f7] Starting scan for ble modules.
03-10 16:12:20.914 13911 11611 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:12:22.917 13911 13911 I LoggingProviderImpl: [main] Scan Failed with error code: 2
03-10 16:12:22.982 13911 13911 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:12:43.472 13911 27931 D BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=6 mScannerId=-1
03-10 16:12:43.659 13911 27931 D BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=8 mScannerId=-1
03-10 16:12:43.735 13911 27931 D BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=-1
03-10 16:12:43.811 13911 27931 D BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=10 mScannerId=-1
03-10 16:12:47.014 13911 11611 I LoggingProviderImpl: [pool-30-f7] Starting scan for ble modules.
03-10 16:12:47.021 13911 11611 D BluetoothAdapter: isLeEnabled(): ON
03-10 16:12:47.026 13911 14084 D BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=6 mScannerId=0

我有以下问题,如果有人能回答这些问题,那将非常有帮助:

  1. 我们如何获取有关绑定线程的信息?我们需要的信息是应用程序实例的绑定线程池中有多少线程。
  2. 有什么方法可以获得我的应用程序的活页夹性能统计信息吗?
  3. 是否有任何选项可用于获取应用程序发出和完成的 IPC 调用总数?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。