【问题标题】:Bluetooth Scans Devices, But Cannot Write to Device After Frequent Scanning蓝牙扫描设备,但频繁扫描后无法写入设备
【发布时间】:2020-06-03 08:13:34
【问题描述】:

用例:我编写了一个安全应用程序,它与蓝牙 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 秒扫描一个多小时。结果相似,因为我可以扫描设备,但不能通过向其写入数据来连接和切换灯。在新的切换应用程序中出现问题后,我切换到我的主安全应用程序,该应用程序一直在运行,并且运行良好。显然,该问题仅影响扫描过于频繁的应用程序。不过,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);
}

【问题讨论】:

    标签: android bluetooth bluetooth-lowenergy


    【解决方案1】:

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

    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;
      });
    });
    }
    

    【讨论】:

    • 这段代码只是如何在颤振中运行扫描......我显然知道如何运行扫描哈哈。问题是为什么在我的应用程序中扫描后,我被阻止写入设备。这似乎是某种类型的安全措施。我实际上正在使用 java,但很高兴看到一些颤动 - 我正在努力学习。尽管如此 - 感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    相关资源
    最近更新 更多