使用RxAndroidBLE库。
规格:macOS Sierra版本10.12.6,Android studio 3.0.1,手机型号:Moto E,Android版本5.0.2
当我尝试连接2台BLE设备时,出现以下BleDisconnectedException问题,请建议:
在调试时,已实现的问题出现在connection.subscribe ..:
RxBleLog.setLogLevel(RxBleLog.VERBOSE);
Observable<ScanResult> bleSharedScanner
= rxBleClient.scanBleDevices(
new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.build(),
new ScanFilter.Builder()
.build()
).share();
scanSubscription = bleSharedScanner.subscribe(
scanResult -> {
if (scanResult.getBleDevice().getName() != null &&
scanResult.getBleDevice().getName().startsWith("MOB")) {
final RxBleDevice rxBleDevice = scanResult.getBleDevice();
System.out.println("Connecting....");
// connect to device
final rx.Observable<RxBleConnection> connection = rxBleDevice.establishConnection(false);
connection.subscribe(rxBleConnection -> {
System.out.println("Connected....");
},
throwable -> {
throwable.printStackTrace();
System.out.println("Disconnected...");
}
);
}
},
throwable -> subject.onError(throwable)
);
低于错误:
D/RxBle#ClientOperationQueue: QUEUED ScanOperationApi21(439507796)
D/RxBle#ClientOperationQueue: STARTED ScanOperationApi21(439507796)
I/RxBle#QueueOperation: Scan operation is requested to start.
D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
D/RxBle#ClientOperationQueue: FINISHED ScanOperationApi21(439507796)
D/BluetoothLeScanner: onScanResult() - ScanResult{mDevice=22:22:18:53:D5:1C, mScanRecord=ScanRecord [mAdvertiseFlags=26, mServiceUuids=[8ec1e808-67c9-11e6-8b77-86f30ca893d3], mManufacturerSpecificData={}, mServiceData={}, mTxPowerLevel=-2147483648, mDeviceName=US1], mRssi=-66, mTimestampNanos=466666244618}
D/BluetoothLeScanner: onScanResult() - ScanResult{mDevice=22:22:B4:4B:2E:67, mScanRecord=ScanRecord [mAdvertiseFlags=26, mServiceUuids=[8ec1e808-67c9-11e6-8b77-86f30ca893d3], mManufacturerSpecificData={}, mServiceData={}, mTxPowerLevel=-2147483648, mDeviceName=US2], mRssi=-59, mTimestampNanos=466756052951}
D/BluetoothLeScanner: onScanResult() - ScanResult{mDevice=67:2A:F2:1C:D5:5E, mScanRecord=ScanRecord [mAdvertiseFlags=26, mServiceUuids=[00001805-0000-1000-8000-00805f9b34fb], mManufacturerSpecificData={}, mServiceData={}, mTxPowerLevel=-2147483648, mDeviceName=111-1], mRssi=-73, mTimestampNanos=466785244253}
I/System.out: Connecting....
D/BluetoothLeScanner: onScanResult() - ScanResult{mDevice=7B:98:4E:7A:34:97, mScanRecord=ScanRecord [mAdvertiseFlags=2, mServiceUuids=[00001805-0000-1000-8000-00805f9b34fb], mManufacturerSpecificData={}, mServiceData={}, mTxPowerLevel=-2147483648, mDeviceName=111-2], mRssi=-58, mTimestampNanos=466933499931}
D/RxBle#ClientOperationQueue: QUEUED ConnectOperation(683111114)
D/RxBle#ClientOperationQueue: STARTED ConnectOperation(683111114)
I/System.out: Connecting....
D/RxBle#ClientOperationQueue: QUEUED ConnectOperation(868920235)
V/RxBle#BleConnectionCompat: Connecting without reflection
D/BluetoothGatt: connect() - device: 67:2A:F2:1C:D5:5E, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=87a04fa1-ac28-4e0b-965a-e43bc3a5cc2a
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
D/BluetoothLeScanner: onScanResult() - ScanResult{mDevice=28:43:81:F1:E3:C2, mScanRecord=ScanRecord [mAdvertiseFlags=-1, mServiceUuids=null, mManufacturerSpecificData={6=[1, 9, 32, 0, 24, 67, -125, -102, 90, -34, 56, -22, 127, 22, -113, -94, -55, -22, -37, -12, -111, -124, -36, 11, 111, 14, -18]}, mServiceData={}, mTxPowerLevel=-2147483648, mDeviceName=null], mRssi=-103, mTimestampNanos=467087746285}
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=67:2A:F2:1C:D5:5E
D/RxBle#BluetoothGatt: onConnectionStateChange newState=2 status=0
D/RxBle#ClientOperationQueue: FINISHED ConnectOperation(683111114)
D/RxBle#ClientOperationQueue: STARTED ConnectOperation(868920235)
V/RxBle#BleConnectionCompat: Connecting without reflection
I/System.out: Connected....
D/BluetoothGatt: connect() - device: 7B:98:4E:7A:34:97, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=1729dc19-b4ee-4088-b9a3-507c0b80c0bb
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=7B:98:4E:7A:34:97
D/RxBle#BluetoothGatt: onConnectionStateChange newState=2 status=0
D/BluetoothGatt: onClientConnectionState() - status=62 clientIf=7 device=7B:98:4E:7A:34:97
D/RxBle#BluetoothGatt: onConnectionStateChange newState=0 status=62
D/RxBle#ClientOperationQueue: FINISHED ConnectOperation(868920235)
W/System.err: BleDisconnectedException{bluetoothDeviceAddress='7B:98:4E:7A:34:97'}
W/System.err: at com.polidea.rxandroidble.internal.connection.RxBleGattCallback$2.onConnectionStateChange(RxBleGattCallback.java:76)
W/System.err: at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:181)
W/System.err: at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
W/System.err: at android.os.Binder.execTransact(Binder.java:446)
I/System.out: Disconnected...
I/RxBle#ConnectionOperationQueue: Connection operations queue to be terminated (7B:98:4E:7A:34:97)
D/RxBle#Executors$RunnableAdapter: Terminated.
D/RxBle#ClientOperationQueue: QUEUED DisconnectOperation(810732953)
D/RxBle#ClientOperationQueue: STARTED DisconnectOperation(810732953)
D/BluetoothManager: getConnectionState()
D/BluetoothManager: getConnectedDevices
D/BluetoothGatt: close()
D/BluetoothGatt: unregisterApp() - mClientIf=7
D/RxBle#ClientOperationQueue: FINISHED DisconnectOperation(810732953)
答案 0 :(得分:0)
status=62
为0x3E
,表示the AOSP source code中的#define GATT_CONN_FAIL_ESTABLISH HCI_ERR_CONN_FAILED_ESTABLISHMENT/* 0x03E connection fail to establish */
。
正如您所看到的,这不是太描述性。问题在于Android BLE堆栈的较低级别。
我没有在代码中看到您停止扫描的任何位置。您可以做的是在尝试连接到第二个设备之前停止扫描。 BLE堆栈的紧张程度更低: