android - 蓝牙扫描设备,频繁扫描后无法写入设备

标签 android bluetooth bluetooth-lowenergy

用例:我编写了一个安全应用程序,它与蓝牙 Mesh 一起运行。网格包含自定义灯和警报。为了检查网状系统的状态,我的应用程序扫描最近的设备之一(通过 RSSI 强度)以查看蓝牙灯/警报在其扫描响应数据包中包含的数据。因为这是一款安全应用,我的应用每 15 秒扫描一次,以确保它始终拥有最新信息。向警方报告安全威胁,因此尽可能及时了解最新情况很重要。秒很重要。应用程序运行几周后,该应用程序能够扫描数据,但无法再连接到设备。 “清除”警报的唯一方法是使用应用程序,因此必须能够在需要时写入设备。

我的问题:为什么会这样?我怎么可能扫描设备,但不再写入设备?

受影响的设备 Logcat: 我能够找到处于此问题状态的设备并在尝试写入设备时收集 logcat。如您所见,我能够连接、查看服务,甚至更新 MTU。但无法写入设备。我的应用程序会自动尝试 5 次,这是最后一次尝试。请注意,我尝试打开和关闭蓝牙。重启平板电脑似乎确实解决了这个问题。不幸的是,我没有尝试先关闭并重新启动应用程序....

E/Bluetooth LE: Best RSSI Position set
E/Bluetooth LE: Bluetooth Device saved                      
D/BluetoothGatt: connect() - device: 00:A0:50:E1:88:51, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=3b720df8-2e40-47bf-aa2c-99c0c1fba362
E/Bluetooth LE: Attempting To Connect to GATT
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=00:A0:50:E1:88:51
D/BluetoothGatt: discoverServices() - device: 00:A0:50:E1:88:51
D/BluetoothGatt: onConnectionUpdated() - Device=00:A0:50:E1:88:51 interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt: onSearchComplete() = Device=00:A0:50:E1:88:51 Status=0
E/Bluetooth: GATT Services count: 3
E/Bluetooth: Service Found - UUID: 00001800-0000-1000-8000-00805f9b34fb
E/Bluetooth: Service Found - UUID: 00001801-0000-1000-8000-00805f9b34fb
E/Bluetooth: Service Found - UUID: 0000cbbb-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: configureMTU() - device: 00:A0:50:E1:88:51 mtu: 50
D/BluetoothGatt: onConnectionUpdated() - Device=00:A0:50:E1:88:51 interval=39 latency=0 timeout=500 status=0
D/BluetoothGatt: onConfigureMTU() - Device=00:A0:50:E1:88:51 mtu=50 status=0
D/Surface: Surface::disconnect(this=0x74241f7000,api=1)
D/View: [Warning] assignParent to null: this = android.widget.LinearLayout{f04b3f V.E...... ......ID 0,0-333,75}
D/BluetoothManager: getConnectedDevices
D/BluetoothManager: getConnectedDevices
E/Bluetooth Gatt: Closing Gatt
D/BluetoothGatt: cancelOpen() - device: 00:A0:50:E1:88:51
D/BluetoothGatt: close()
D/BluetoothGatt: unregisterApp() - mClientIf=6
D/BluetoothManager: getConnectedDevices
W/package.name: type=1400 audit(0.0:38806): avc: denied { search } for name="custom" dev="dm-0" ino=3890 scontext=u:r:untrusted_app:s0:c100,c256,c512,c768 tcontext=u:object_r:custom_file:s0 tclass=dir permissive=0
D/ViewRootImpl[Toast]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
D/Surface: Surface::connect(this=0x74241f7000,api=1)
D/Surface: Surface::allocateBuffers(this=0x74241f7000)
D/BluetoothAdapter: isLeEnabled(): ON
D/BluetoothLeScanner: onScannerRegistered() - status=6 scannerId=-1 mScannerId=0
D/BluetoothAdapter: isLeEnabled(): ON
D/BluetoothLeScanner: could not find callback wrapper

进一步测试:为了进行一些测试,我制作了一个可以打开和关闭灯的小应用程序。我将应用程序设置为每 3 秒扫描一次,持续了一个多小时。结果是相似的,因为我可以扫描设备,但不能通过向它写入数据来连接和切换灯。在新的 toggle-app 出现问题后,我切换到我的主要安全应用程序,它一直在运行,并且运行良好。显然,该问题仅影响扫描过于频繁的应用程序。 logcat 中的差异虽然让我有点困扰 - 问题发生后,logcat 将 onClientRegistered 状态显示为 133,这在一定程度上是有道理的。主应用程序没有显示。请问这是两个不同的问题吗?

D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothLeScanner: Start Scan with callback
D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
D/InputTransport: Input channel constructed: fd=88
D/ViewRootImpl@dc2b515[Toast]: setView = android.widget.LinearLayout{251b02a V.E...... ......I. 0,0-0,0} TM=true MM=false
V/Toast: Text: Uogg in android.widget.Toast$TN@af0fe1b
D/ViewRootImpl@dc2b515[Toast]: dispatchAttachedToWindow
D/ViewRootImpl@dc2b515[Toast]: Relayout returned: old=[0,63][1080,2094] new=[247,1810][832,1926] result=0x7 surface={valid=true 509882912768} changed=true
D/OpenGLRenderer: eglCreateWindowSurface = 0x76b4a19080, 0x76b763d010
D/BluetoothAdapter: STATE_ON
D/BluetoothGatt: connect() - device: 00:A0:50:DF:22:58, auto: false
D/BluetoothAdapter: isSecureModeEnabled
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=0241923a-48e6-426a-81c0-acbea6690766
D/BluetoothGatt: onClientRegistered() - status=133 clientIf=0
E/Bluetooth LE: Disconnected from GATT
E/Bluetooth Gatt: Closing Gatt
D/BluetoothGatt: cancelOpen() - device: 00:A0:50:DF:22:58
D/BluetoothGatt: close()
D/BluetoothGatt: unregisterApp() - mClientIf=0

飞行模式:出于某种原因,打开和关闭蓝牙没有任何作用,但打开飞行模式解决了 onClientRegistered() - 状态 133 问题。我不知道为什么会这样。飞行模式是否会清除所有蓝牙缓存或类似内容?目前,我的尝试是每 6 小时关闭/打开蓝牙,但这似乎不起作用。不过我没有清除缓存。

除了让客户每晚关闭他们的设备(我不能保证他们会这样做)之外,似乎没有任何办法可以解决问题。欢迎任何想法。

编辑:我关闭了设备的蓝牙并注意到我的 logcat 在尝试切换其中一个蓝牙灯时显示 D/BluetoothAdapter: isLeEnabled(): ON。在尝试扫描并连接到设备后,我还看到这出现在我受影响的设备的 logcat 中。我想知道是否需要同时关闭蓝牙和蓝牙 LE 以刷新所有内容。在这一点上打开和关闭蓝牙没有任何帮助。我正在使用 BluetoothLE 扫描仪,所以它会有些意义。有点奇怪的切换蓝牙不会影响 LE。搜索谷歌显示如下(Full Code/Credit Here):

@SystemApi
public boolean isLeEnabled() {
    final int state = getLeState();
    if (DBG) {
        Log.d(TAG, "isLeEnabled(): " + BluetoothAdapter.nameForState(state));
    }
    return (state == BluetoothAdapter.STATE_ON
            || state == BluetoothAdapter.STATE_BLE_ON
            || state == BluetoothAdapter.STATE_TURNING_ON
            || state == BluetoothAdapter.STATE_TURNING_OFF);
}

最佳答案

我认为你可以使用这段代码:

Future<void> scanPrinters() async {
// CHECK TO PUT INIT
flutterBlue.startScan(timeout: Duration(seconds: 4));
flutterBlue.scanResults.listen((results) {
  for (blueB.ScanResult r in results) {
    print('Device ${r.device.name} found!');
  }
});

printerManager.startScan(Duration(seconds: 3));    
printerManager.scanResults.listen((results) {
  print("dipositi" + results.toString());
  setState(() {
    printers = results;
  });
});
}

关于android - 蓝牙扫描设备,频繁扫描后无法写入设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60295609/

相关文章:

android - 无法解析 Android Studio 中的依赖项

iOS Corebluetooth 广告和扫描,同时应用程序在后台

Android 蓝牙 Inputstream.read 从不接收数据

bluetooth-lowenergy - 代号一 如何添加BLE扩展

android - 如何使用 RSSI 改进蓝牙距离测量?

java - Android BLE 接近通知

android - Eclipse android phonegap config.xml 不工作?

android USB调试消失了,不再工作了

c++ - 使用 Windows 套接字进行蓝牙编程 - 范围内的设备

android - setSupportActionBar() 抛出空指针异常