gpt4 book ai didi

Android BluetoothLeScanner startScan PendingIntent 28 次后在系统范围内失败

转载 作者:行者123 更新时间:2023-12-02 03:12:54 32 4
gpt4 key购买 nike

我有一个 Android 应用用例,需要“连续”扫描 BLE 设备。
仅此一项要求就可能让我成为受虐狂,我猜这会让 Android 成为我的施虐狂。

我知道[我相信]仍未正式记录“30 秒内扫描不超过 5 次”和“扫描时间超过 10 分钟”、“没有 ScanFilter 则没有扫描结果”、背景问题以及其他一些。

为了缓解这些问题,我使用了类似于 AltBeacon、Nordic Sc​​anner-Compat、SweetBlue 和其他一些技巧。

我认为这些都不是这篇文章的问题。

输入我当前的问题。

一般来说,我的应用程序使用 PendingIntent 开始扫描 3.1 秒,然后停止(逻辑“暂停”)3.1 秒,然后重复(逻辑“恢复”)并在用户明确退出应用程序后硬停止。

下面是 8.1.0 Nexus 6P 重新启动并打开蓝牙、启动我的应用程序并开始扫描的日志。
我不知道为什么操作系统在开始扫描时调用gatt_api,以及为什么它在停止时提示Context not found;也许这些是线索?

2019-07-18 17:51:53.462 13662-13662/? D/AdapterServiceConfig: Adding GattService
2019-07-18 17:51:53.496 13662-13676/? D/BluetoothAdapterService: setProfileServiceState() - Starting service com.android.bluetooth.gatt.GattService
2019-07-18 17:51:53.497 13662-13662/? I/BtGatt.JNI: classInitNative(L871): classInitNative: Success!
2019-07-18 17:51:53.498 13662-13662/? D/BtGatt.DebugUtils: handleDebugAction() action=null
2019-07-18 17:51:53.499 13662-13662/? D/BtGatt.GattService: Received start request. Starting profile...
2019-07-18 17:51:53.499 13662-13662/? I/bt_btif: get_profile_interface: id = gatt
2019-07-18 17:51:53.504 13662-13662/? D/BluetoothAdapterService: processProfileServiceStateChanged() - serviceName=com.android.bluetooth.gatt.GattService isTurningOn=false isTurningOff=false isBleTurningOn=true isBleTurningOff=false
2019-07-18 17:51:53.504 13662-13662/? D/BluetoothAdapterService: GattService is started
2019-07-18 17:51:54.251 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:51:54.251 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1027)] allocated gatt_if=3
2019-07-18 17:51:54.274 4384-4435/? D/BluetoothManagerService: Binding Bluetooth GATT service
2019-07-18 17:51:54.277 4384-4384/? D/BluetoothManagerService: BluetoothServiceConnection: com.android.bluetooth.gatt.GattService
2019-07-18 17:51:54.278 4384-4435/? D/BluetoothManagerService: BluetoothGatt Service is Up
2019-07-18 17:51:54.376 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:51:54.376 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1027)] allocated gatt_if=4
2019-07-18 17:51:54.653 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:51:54.653 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1027)] allocated gatt_if=5
2019-07-18 17:51:54.685 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:51:54.685 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1027)] allocated gatt_if=6

2019-07-18 17:52:00.800 13349-13349/com.myapp V/MyBleScanner: resume: #GATT +mBleScannerInternal.startScan(...)
2019-07-18 17:52:00.818 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:52:00.818 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1027)] allocated gatt_if=6
2019-07-18 17:52:00.818 13349-13349/com.myapp V/MyBleScanner: resume: #GATT mBleScannerInternal.startScan(...); result=SUCCESS(0)
2019-07-18 17:52:00.836 13349-13349/com.myapp V/MyBleScanner: resume: #GATT -mBleScannerInternal.startScan(...)
2019-07-18 17:52:00.837 13349-13349/com.myapp E/MyBleScanner: resume: #GATT mBleScannerInternalStartScanCount=1, elapsedMillisSinceBleScannerInternalStartScanFirstTime=0

2019-07-18 17:52:03.942 13349-13349/com.myapp V/MyBleScanner: pause: #GATT +mBleScannerInternal.stopScan(...)
2019-07-18 17:52:03.959 13662-13674/? E/BtGatt.ContextMap: Context not found for info com.android.bluetooth.gatt.GattService$PendingIntentInfo@2f1237f
2019-07-18 17:52:03.960 13349-13349/com.myapp V/MyBleScanner: pause: #GATT -mBleScannerInternal.stopScan(...)
2019-07-18 17:52:03.966 13349-13349/com.myapp I/MyBleScanner: pause: #GATT mScannedDevices(36)={"31:2C:26:FA:91:5E"=null, "0E:06:E5:5D:1F:9D"=null, "0E:05:A0:00:28:E3"=null, "16:05:05:B8:E9:51"=null, "0E:0A:B0:00:13:2E"=null, "0E:0E:A0:05:C9:B4"=null, "74:F0:1C:42:2A:6C"=null, "0E:0C:00:00:0A:14"=null, "0E:0A:00:00:23:38"=null, "0E:05:A0:00:13:AC"=null, "0E:0E:A0:03:CC:16"=null, "0E:0E:A0:02:22:78"=null, "30:8C:FB:D4:BD:B1"=null, "30:8C:FB:7A:55:21"=null, "CD:A3:87:DA:15:D6"=null, "0E:0E:A0:03:C6:D6"=null, "0E:05:A0:00:32:6E"=null, "0E:0E:A0:05:46:FD"=null, "0E:0E:A0:01:0C:DF"=null, "0E:0E:A0:01:14:36"=null, "0E:0E:A0:01:14:56"=null, "0E:0E:A0:03:F7:1A"=null, "0E:0E:43:21:FE:0E"=null, "0E:0E:A0:0E:E2:72"=null, "0E:0E:A0:04:07:1F"=null, "F5:2A:1E:90:43:D7"=null, "0B:F8:B4:90:A2:3A"=null, "0E:0E:A0:00:1E:DF"=null, "0E:05:A0:00:61:B1"=null, "0E:0E:A0:03:DC:D3"=null, "0E:05:A0:00:84:12"=null, "0E:05:A0:00:84:13"=null, "0E:06:E5:59:D8:B3"=null, "0E:0A:A0:01:3D:13"=null, "2F:41:6D:A2:92:C6"=null, "0E:0A:A0:01:3A:69"=null}
2019-07-18 17:52:04.595 13662-13682/? E/BtGatt.GattService: Exception: android.app.PendingIntent$CanceledException

2019-07-18 17:52:07.076 13349-13349/com.myapp V/MyBleScanner: resume: #GATT +mBleScannerInternal.startScan(...)
2019-07-18 17:52:07.111 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:52:07.112 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1027)] allocated gatt_if=7
2019-07-18 17:52:07.112 13349-13349/com.myapp V/MyBleScanner: resume: #GATT mBleScannerInternal.startScan(...); result=SUCCESS(0)
2019-07-18 17:52:07.127 13349-13349/com.myapp V/MyBleScanner: resume: #GATT -mBleScannerInternal.startScan(...)
2019-07-18 17:52:07.127 13349-13349/com.myapp E/MyBleScanner: resume: #GATT mBleScannerInternalStartScanCount=2, elapsedMillisSinceBleScannerInternalStartScanFirstTime=6291

2019-07-18 17:52:10.231 13349-13349/com.myapp V/MyBleScanner: pause: #GATT +mBleScannerInternal.stopScan(...)
2019-07-18 17:52:10.238 13662-13674/? E/BtGatt.ContextMap: Context not found for info com.android.bluetooth.gatt.GattService$PendingIntentInfo@1377141
2019-07-18 17:52:10.239 13349-13349/com.myapp V/MyBleScanner: pause: #GATT -mBleScannerInternal.stopScan(...)
2019-07-18 17:52:10.242 13349-13349/com.myapp I/MyBleScanner: pause: #GATT mScannedDevices(37)={"31:2C:26:FA:91:5E"=null, "0E:0E:A0:00:18:EC"=null, "0E:06:E5:5D:1F:9D"=null, "0E:05:A0:00:28:E3"=null, "16:05:05:B8:E9:51"=null, "0E:0A:B0:00:13:2E"=null, "0E:0E:A0:05:C9:B4"=null, "74:F0:1C:42:2A:6C"=null, "0E:0A:00:00:23:38"=null, "0E:0C:00:00:0A:14"=null, "0E:05:A0:00:13:AC"=null, "0E:0E:A0:02:22:78"=null, "30:8C:FB:D4:BD:B1"=null, "30:8C:FB:7A:55:21"=null, "CD:A3:87:DA:15:D6"=null, "0E:0E:A0:03:C6:D6"=null, "0E:0E:A0:05:46:FD"=null, "0E:0E:A0:03:3B:16"=null, "0E:0E:A0:01:14:36"=null, "CD:31:C7:3B:84:0F"=null, "0E:0E:A0:02:0B:C1"=null, "DA:A3:9B:E8:3B:E8"=null, "0E:0E:A0:02:24:97"=null, "0E:0E:A0:03:F7:1A"=null, "0E:0E:43:21:FE:0E"=null, "0E:0E:A0:0E:E2:72"=null, "0B:F8:B4:90:A2:3A"=null, "F5:2A:1E:90:43:D7"=null, "0E:0E:A0:00:1E:DF"=null, "0E:05:A0:00:61:B1"=null, "0E:0E:A0:03:DC:D3"=null, "0E:05:A0:00:84:12"=null, "0E:05:A0:00:84:13"=null, "0E:0A:A0:01:3D:13"=null, "0E:06:E5:59:D8:B3"=null, "2F:41:6D:A2:92:C6"=null, "0E:0A:A0:01:3A:69"=null}
2019-07-18 17:52:10.483 13662-13682/? E/BtGatt.GattService: Exception: android.app.PendingIntent$CanceledException

...
日志按预期重复...除了可能是分配的 gatt_if=8 的信息,然后是 9、10、...、31...
...

2019-07-18 17:54:43.869 13349-13349/com.myapp V/MyBleScanner: resume: #GATT +mBleScannerInternal.startScan(...)
2019-07-18 17:54:43.893 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:54:43.893 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1027)] allocated gatt_if=32
2019-07-18 17:54:43.893 13349-13349/com.myapp V/MyBleScanner: resume: #GATT mBleScannerInternal.startScan(...); result=SUCCESS(0)
2019-07-18 17:54:43.912 13349-13349/com.myapp V/MyBleScanner: resume: #GATT -mBleScannerInternal.startScan(...)
2019-07-18 17:54:43.913 13349-13349/com.myapp E/MyBleScanner: resume: #GATT mBleScannerInternalStartScanCount=27, elapsedMillisSinceBleScannerInternalStartScanFirstTime=163077
2019-07-18 17:54:43.952 13662-13682/? E/BtGatt.GattService: Exception: android.app.PendingIntent$CanceledException

2019-07-18 17:54:47.021 13349-13349/com.myapp V/MyBleScanner: pause: #GATT +mBleScannerInternal.stopScan(...)
2019-07-18 17:54:47.033 13662-13703/? E/BtGatt.ContextMap: Context not found for info com.android.bluetooth.gatt.GattService$PendingIntentInfo@e09708f
2019-07-18 17:54:47.036 13349-13349/com.myapp V/MyBleScanner: pause: #GATT -mBleScannerInternal.stopScan(...)
2019-07-18 17:54:47.037 13349-13349/com.myapp I/MyBleScanner: pause: #GATT mScannedDevices(37)={"0E:0E:A0:02:0D:1A"=null, "0E:06:E5:5D:1F:9D"=null, "0E:0E:A0:00:18:EC"=null, "16:05:05:B8:E9:51"=null, "0E:0E:FF:F6:BE:30"=null, "74:F0:1C:42:2A:6C"=null, "0E:0C:00:00:0A:14"=null, "0E:0A:00:00:23:38"=null, "0E:0E:A0:03:CC:16"=null, "0E:0E:A0:01:0B:E6"=null, "0E:0E:A0:02:22:78"=null, "30:8C:FB:D4:BD:B1"=null, "30:8C:FB:7A:55:21"=null, "CD:A3:87:DA:15:D6"=null, "0E:05:A0:00:32:6E"=null, "0E:0E:A0:03:28:7B"=null, "0E:0E:A0:01:0C:DF"=null, "CD:31:C7:3B:84:0F"=null, "DA:A3:9B:E8:3B:E8"=null, "0E:0E:A0:01:14:56"=null, "0E:0E:A0:02:24:97"=null, "0E:0E:A0:02:15:F8"=null, "37:AB:CB:F6:2D:70"=null, "0E:0E:43:21:FE:0E"=null, "24:B9:20:97:DC:87"=null, "0E:0E:A0:04:6B:34"=null, "0E:0E:A0:04:07:1F"=null, "0B:F8:B4:90:A2:3A"=null, "F5:2A:1E:90:43:D7"=null, "0E:05:A0:00:61:B0"=null, "0E:0E:A0:03:21:34"=null, "0E:05:A0:00:61:B1"=null, "0E:05:A0:00:84:13"=null, "0E:06:E5:59:D8:B3"=null, "0E:0A:A0:01:3D:13"=null, "2F:41:6D:A2:92:C6"=null, "0E:0A:A0:01:3A:69"=null}
2019-07-18 17:54:47.547 13662-13662/? D/BtGatt.ScanManager: awakened up at time 187337376
2019-07-18 17:54:47.552 13662-13682/? E/BtGatt.GattService: Exception: android.app.PendingIntent$CanceledException

2019-07-18 17:54:50.143 13349-13349/com.myapp V/MyBleScanner: resume: #GATT +mBleScannerInternal.startScan(...)
2019-07-18 17:54:50.173 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:54:50.173 13662-13697/? E/bt_stack: [ERROR:gatt_api.cc(1032)] can't Register GATT client, MAX client reached: 32
2019-07-18 17:54:50.173 13662-13697/? E/bt_btif: Register with GATT stack failed.
2019-07-18 17:54:50.173 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:54:50.174 13349-13349/com.myapp V/MyBleScanner: resume: #GATT mBleScannerInternal.startScan(...); result=SUCCESS(0)
2019-07-18 17:54:50.174 13662-13697/? E/bt_stack: [ERROR:gatt_api.cc(1032)] can't Register GATT client, MAX client reached: 32
2019-07-18 17:54:50.174 13662-13697/? E/bt_btif: Register with GATT stack failed.
2019-07-18 17:54:50.187 13349-13349/com.myapp V/MyBleScanner: resume: #GATT -mBleScannerInternal.startScan(...)
2019-07-18 17:54:50.188 13349-13349/com.myapp E/MyBleScanner: resume: #GATT mBleScannerInternalStartScanCount=28, elapsedMillisSinceBleScannerInternalStartScanFirstTime=169351

...
总是我的第 28 次调用 startScan(除非可能有其他应用程序已扫描),大约 170 秒后,操作系统开始报告 gatt_api 错误无法注册GATT客户端,最大客户端达到:32,此时:

  1. 我得到 0 返回值,并且没有 onScanFailed 回调表明扫描失败
  2. 我没有收到显示任何已扫描 BLE 设备的 onScanResult 回调

...

2019-07-18 17:54:53.291 13349-13349/com.myapp V/MyBleScanner: pause: #GATT +mBleScannerInternal.stopScan(...)
2019-07-18 17:54:53.303 13662-13715/? E/BtGatt.ContextMap: Context not found for info com.android.bluetooth.gatt.GattService$PendingIntentInfo@7eaaf1c
2019-07-18 17:54:53.304 13349-13349/com.myapp V/MyBleScanner: pause: #GATT -mBleScannerInternal.stopScan(...)
2019-07-18 17:54:53.304 13349-13349/com.myapp I/MyBleScanner: pause: #GATT mScannedDevices(0)={}
2019-07-18 17:54:56.410 13349-13349/com.myapp V/MyBleScanner: resume: #GATT +mBleScannerInternal.startScan(...)
2019-07-18 17:54:56.439 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:54:56.439 13662-13697/? E/bt_stack: [ERROR:gatt_api.cc(1032)] can't Register GATT client, MAX client reached: 32
2019-07-18 17:54:56.439 13662-13697/? E/bt_btif: Register with GATT stack failed.
2019-07-18 17:54:56.439 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:54:56.439 13662-13697/? E/bt_stack: [ERROR:gatt_api.cc(1032)] can't Register GATT client, MAX client reached: 32
2019-07-18 17:54:56.439 13662-13697/? E/bt_btif: Register with GATT stack failed.
2019-07-18 17:54:56.440 13349-13349/com.myapp V/MyBleScanner: resume: #GATT mBleScannerInternal.startScan(...); result=SUCCESS(0)
2019-07-18 17:54:56.458 13349-13349/com.myapp V/MyBleScanner: resume: #GATT -mBleScannerInternal.startScan(...)
2019-07-18 17:54:56.458 13349-13349/com.myapp E/MyBleScanner: resume: #GATT mBleScannerInternalStartScanCount=29, elapsedMillisSinceBleScannerInternalStartScanFirstTime=175622
2019-07-18 17:54:59.562 13349-13349/com.myapp V/MyBleScanner: pause: #GATT +mBleScannerInternal.stopScan(...)
2019-07-18 17:54:59.576 13662-13673/? E/BtGatt.ContextMap: Context not found for info com.android.bluetooth.gatt.GattService$PendingIntentInfo@bef4725
2019-07-18 17:54:59.576 13349-13349/com.myapp V/MyBleScanner: pause: #GATT -mBleScannerInternal.stopScan(...)
2019-07-18 17:54:59.577 13349-13349/com.myapp I/MyBleScanner: pause: #GATT mScannedDevices(0)={}
2019-07-18 17:55:02.679 13349-13349/com.myapp V/MyBleScanner: resume: #GATT +mBleScannerInternal.startScan(...)
2019-07-18 17:55:02.697 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:55:02.697 13662-13697/? E/bt_stack: [ERROR:gatt_api.cc(1032)] can't Register GATT client, MAX client reached: 32
2019-07-18 17:55:02.697 13662-13697/? E/bt_btif: Register with GATT stack failed.
2019-07-18 17:55:02.697 13662-13697/? I/bt_stack: [INFO:gatt_api.cc(1004)] GATT_Register
2019-07-18 17:55:02.697 13662-13697/? E/bt_stack: [ERROR:gatt_api.cc(1032)] can't Register GATT client, MAX client reached: 32
2019-07-18 17:55:02.697 13662-13697/? E/bt_btif: Register with GATT stack failed.
2019-07-18 17:55:02.697 13349-13349/com.myapp V/MyBleScanner: resume: #GATT mBleScannerInternal.startScan(...); result=SUCCESS(0)
2019-07-18 17:55:02.707 13349-13349/com.myapp V/MyBleScanner: resume: #GATT -mBleScannerInternal.startScan(...)
2019-07-18 17:55:02.707 13349-13349/com.myapp E/MyBleScanner: resume: #GATT mBleScannerInternalStartScanCount=30, elapsedMillisSinceBleScannerInternalStartScanFirstTime=181871
2019-07-18 17:55:05.810 13349-13349/com.myapp V/MyBleScanner: pause: #GATT +mBleScannerInternal.stopScan(...)
2019-07-18 17:55:05.822 13662-13703/? E/BtGatt.ContextMap: Context not found for info com.android.bluetooth.gatt.GattService$PendingIntentInfo@82cc0fa
2019-07-18 17:55:05.822 13349-13349/com.myapp V/MyBleScanner: pause: #GATT -mBleScannerInternal.stopScan(...)
2019-07-18 17:55:05.823 13349-13349/com.myapp I/MyBleScanner: pause: #GATT mScannedDevices(0)={}

这里似乎发生了某种分配的 gatt_if 泄漏,但我只是调用 startScan(..., mPendingIntent)stopScan( ..., mPendingIntent) 具有相同的最终/不可变的 PendingIntent 成员,所以我非常怀疑我的代码中的任何内容是否直接导致“泄漏”。

此时,没有应用程序可以扫描 BLE 设备。不是我的应用程序或 SweetBlue Toolbox、BLE 扫描仪或我尝试过的任何其他第 3 方 BLE 扫描应用程序。

到底为什么会发生这种情况?

当手机确实进入此状态时,我可以通过关闭手机的蓝牙并再次打开来返回到工作状态。

AltBeacon 有一个 BluetoothMedic 类,据说可以检测到此状态并以编程方式关闭蓝牙并再次打开,但我将其添加为依赖项并在其 onScanFailed 回调内部设置断点,但它永远不会调用,这违背了类(class)的整个目的,我很难相信这对他们来说是一个已知的问题。 :/

我希望从一开始就不存在这种泄漏,并且能够无限期地每 3.1 秒停止和启动扫描一次,并可靠地扫描范围内的本地设备。

除此之外,如果必须的话,我必须以编程方式关闭并重新打开蓝牙(是的,每 170 秒一次...欢迎来到 Android BLE 世界的 hell ),但没有有效的 startScan 返回值或 onScanFailed回调无法确定扫描零个设备是否意味着手机已进入这种不良状态,或者周围真的有零个设备。

我确信这段代码在一年多的时间里一直运行良好,直到最近(最多几个月前)才开始出现这种情况。

还有希望吗?

Android BLE 真的很糟糕。 :/

最佳答案

更新(相信正确)

我的最终结论是,Nordic 的 Android-Scanner-Compat-Library 确实创建了一个可停止的 PendingIntent,但问题是它正在调用 PendingIntent.getBroadcast(context, id, Intent, PendingIntent.FLAG_CANCEL_CURRENT) 当它应该调用 PendingIntent.getBroadcast(context, id, Intent, PendingIntent.FLAG_UPDATE_CURRENT)

我在我的 fork 上修复了它,并提出了一个问题,并将提出一个 PR。

原始(接近,但不完全正确)

我相信我找到了答案。

今天我编写了一个独立的应用程序作为简单的测试用例来重现这个问题。 https://github.com/paulpv/AndroidBleStartScanPendingIntentLeak

想想吧,它没有重现。

测试应用程序和我的生产应用程序的一大区别是:

  1. 在测试应用中,我直接调用 android.bluetooth.le.BluetoothLeScanner.startScan(...)
  2. 在我的生产应用中,我使用 NordicSemiconductor 的 Android-Scanner-Compat-Library ( https://github.com/NordicSemiconductor/Android-Scanner-Compat-Library )

我的测试应用程序没有重现该问题,但是当我调整它以使用 Nordic 的 Android-Scanner-Compat 时,我的问题就开始重现。
我撤消了我的更改;没有重现。
我重新做了我的改变;重现。
确凿证据?

查看他们的代码我发现了这一点: https://github.com/NordicSemiconductor/Android-Scanner-Compat-Library/blob/master/scanner/src/main/java/no/nordicsemi/android/support/v18/scanner/BluetoothLeScannerImplOreo.java#L111

final PendingIntent pendingIntent = createStartingPendingIntent(nonNullFilters,
nonNullSettings, context, callbackIntent);
scanner.startScan(nativeFilters, nativeSettings, pendingIntent);

他们实例化自己的 PendingIntent 来包装我的......
...但是他们的代码中没有任何地方可以跟踪他们的 PendingIntent,以便他们可以使用相同的endingIntent 成功调用 stopScan(pendingIntent)

https://github.com/NordicSemiconductor/Android-Scanner-Compat-Library/blob/master/scanner/src/main/java/no/nordicsemi/android/support/v18/scanner/BluetoothLeScannerImplOreo.java#L126

final PendingIntent pendingIntent = createStoppingPendingIntent(context, callbackIntent);
scanner.stopScan(pendingIntent);

这两个pendingIntents不可能匹配,因此操作系统可以正确停止扫描。

我通过向他们的代码分支添加日志记录来确认这一点(在每个 startScanstopScan 附近搜索“pendingIntent=”;请注意,它们是不同的实例,因为以及每个分配的 gatt_if= 中不断增长的数量):

2019-07-19 21:14:51.230 16905-16905/com.testapp V/BleScannerManager: scan: #GATT +bleScanner.startScan(...)
2019-07-19 21:14:58.424 16905-16905/com.testapp V/BleScannerImplOreo: #GATT startScanInternal: pendingIntent=PendingIntent{1dfc67: android.os.BinderProxy@e560a14}
2019-07-19 21:14:58.468 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(948)] GATT_Register 3c2e1fd9-d3fd-6897-a113-863aa50df781
2019-07-19 21:14:58.468 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(968)] allocated gatt_if=7
2019-07-19 21:14:58.478 16905-16905/com.testapp V/BleScannerManager: scan: #GATT bleScanner.startScan(...); result=SUCCESS(0)
2019-07-19 21:14:58.479 16905-16905/com.testapp V/BleScannerManager: scan: #GATT -bleScanner.startScan(...)
2019-07-19 21:14:58.481 16905-16905/com.testapp E/BleScannerManager: #GATT bleScannerStartScanCount=1, elapsedMillisSinceBleScannerStartScanFirstTime=0
2019-07-19 21:15:03.521 1447-1447/? D/BtGatt.ScanManager: awakened up at time 614650173
2019-07-19 21:15:08.250 16905-16905/com.testapp V/BleScannerManager: scan: #GATT +bleScanner.stopScan(...)
2019-07-19 21:15:08.255 16905-16905/com.testapp V/BleScannerImplOreo: #GATT stopScanInternal: pendingIntent=PendingIntent{dc4115f: android.os.BinderProxy@84087ac}
2019-07-19 21:15:08.259 1447-1982/? E/BtGatt.ContextMap: Context not found for info com.android.bluetooth.gatt.GattService$PendingIntentInfo@77d2d25
2019-07-19 21:15:08.260 16905-16905/com.testapp V/BleScannerManager: scan: #GATT -bleScanner.stopScan(...)
2019-07-19 21:15:08.556 1447-1447/? D/BtGatt.ScanManager: awakened up at time 614655208
2019-07-19 21:15:08.570 1447-1554/? E/BtGatt.GattService: Exception: android.app.PendingIntent$CanceledException
2019-07-19 21:15:20.556 16905-16905/com.testapp V/BleScannerManager: scan: #GATT +bleScanner.startScan(...)
2019-07-19 21:15:20.570 16905-16905/com.testapp V/BleScannerImplOreo: #GATT startScanInternal: pendingIntent=PendingIntent{2ce86d6: android.os.BinderProxy@84087ac}
2019-07-19 21:15:20.575 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(948)] GATT_Register 6b444e39-d61a-1d92-52d6-afd6e901b56b
2019-07-19 21:15:20.575 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(968)] allocated gatt_if=8
2019-07-19 21:15:20.576 16905-16905/com.testapp V/BleScannerManager: scan: #GATT bleScanner.startScan(...); result=SUCCESS(0)
2019-07-19 21:15:20.576 16905-16905/com.testapp V/BleScannerManager: scan: #GATT -bleScanner.startScan(...)
2019-07-19 21:15:20.576 16905-16905/com.testapp E/BleScannerManager: #GATT bleScannerStartScanCount=2, elapsedMillisSinceBleScannerStartScanFirstTime=22097
2019-07-19 21:15:23.264 16905-16905/com.testapp V/BleScannerManager: scan: #GATT +bleScanner.stopScan(...)
2019-07-19 21:15:23.269 16905-16905/com.testapp V/BleScannerImplOreo: #GATT stopScanInternal: pendingIntent=PendingIntent{ebafcb0: android.os.BinderProxy@ebb6729}
2019-07-19 21:15:23.272 1447-1982/? E/BtGatt.ContextMap: Context not found for info com.android.bluetooth.gatt.GattService$PendingIntentInfo@3abaefa
2019-07-19 21:15:23.272 16905-16905/com.testapp V/BleScannerManager: scan: #GATT -bleScanner.stopScan(...)
2019-07-19 21:15:24.968 16905-16905/com.testapp V/BleScannerManager: scan: #GATT +bleScanner.startScan(...)
2019-07-19 21:15:24.980 16905-16905/com.testapp V/BleScannerImplOreo: #GATT startScanInternal: pendingIntent=PendingIntent{8b1626b: android.os.BinderProxy@ebb6729}
2019-07-19 21:15:24.990 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(948)] GATT_Register 0bbb0b8f-2c63-dfc2-268d-fbfe39360ddf
2019-07-19 21:15:24.990 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(968)] allocated gatt_if=9
2019-07-19 21:15:24.990 16905-16905/com.testapp V/BleScannerManager: scan: #GATT bleScanner.startScan(...); result=SUCCESS(0)
2019-07-19 21:15:24.990 16905-16905/com.testapp V/BleScannerManager: scan: #GATT -bleScanner.startScan(...)
2019-07-19 21:15:24.991 16905-16905/com.testapp E/BleScannerManager: #GATT bleScannerStartScanCount=3, elapsedMillisSinceBleScannerStartScanFirstTime=26511
2019-07-19 21:15:25.019 1447-1554/? E/BtGatt.GattService: Exception: android.app.PendingIntent$CanceledException
2019-07-19 21:15:25.795 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(948)] GATT_Register 32a50296-06f2-ed4d-f83c-25da059a4a19
2019-07-19 21:15:25.795 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(968)] allocated gatt_if=10
2019-07-19 21:15:26.046 1447-1447/? D/BtGatt.ScanManager: awakened up at time 614672699
2019-07-19 21:15:27.000 16905-16905/com.testapp V/BleScannerManager: scan: #GATT +bleScanner.stopScan(...)
2019-07-19 21:15:27.005 16905-16905/com.testapp V/BleScannerImplOreo: #GATT stopScanInternal: pendingIntent=PendingIntent{40e339d: android.os.BinderProxy@6340012}
2019-07-19 21:15:27.008 1447-1982/? E/BtGatt.ContextMap: Context not found for info com.android.bluetooth.gatt.GattService$PendingIntentInfo@96b43b4
2019-07-19 21:15:27.009 16905-16905/com.testapp V/BleScannerManager: scan: #GATT -bleScanner.stopScan(...)
2019-07-19 21:15:28.679 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(948)] GATT_Register f119757a-00ff-29b4-72e0-952c5e0db695
2019-07-19 21:15:28.679 1447-1941/? I/bt_stack: [INFO:gatt_api.cc(968)] allocated gatt_if=10
2019-07-19 21:15:28.810 1447-1447/? D/BtGatt.ScanManager: awakened up at time 614675462
2019-07-19 21:15:28.822 1447-1554/? E/BtGatt.GattService: Exception: android.app.PendingIntent$CanceledException

看来我必须在他们的存储库上提出问题,或者自己修复它并向他们发送 PR。

感谢那些插话的人;你的问题让我朝着正确的方向寻找!

关于Android BluetoothLeScanner startScan PendingIntent 28 次后在系统范围内失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57104535/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com