建立连接后,RXBleConnection会继续丢弃(状态19)

时间:2019-05-21 14:50:10

标签: android bluetooth rxandroidble

我正在开发一个与两种传感器配对的应用程序,并且这些传感器彼此配对,因此我们开发了自定义的蓝牙通信协议。该连接通常可以正常工作,但是仍然有些错误令我难以修复。

单独使用传感器1配对的效果很好,但是每次我将它们配对时,我都关闭了应用程序,再次将其与第一个传感器配对,在建立连接后,我断开了状态19的连接再试一两次将正确建立连接。我当时以为这是Gatt刷新的问题,但是我已经尝试过这里找到的一种解决方案,并且每次都不断重现此错误。

 fun connectToDevice(device: BraincareDevice, pairColor: Int) {
    BleLogHelper.writeLog("Connecting to ${device.name}")
    isConnecting = true
    val deviceType = if (device is Sensor) DeviceType.SENSOR else DeviceType.DONGLE
    if (deviceType == DeviceType.SENSOR) {
        sensorConnectionSubscription?.dispose()
    } else {
        dongleConnectionSubscription?.dispose()
    }


    val connectionSubscription = device.device.establishConnection(false)
            .flatMapSingle { connection ->

                if (device is Sensor) {
                    sensorConnection = connection
                    connectedSensor = device
                } else if (device is Dongle) {
                    dongleConnection = connection
                    connectedDongle = device
                }

                connection.queue(CustomRefresh())
                        ?.observeOn(Schedulers.io())
                        ?.doOnComplete{
                            BleLogHelper.writeLog("GATT REFRESHED")
                        }
                        ?.subscribe ({
                            BleLogHelper.writeLog("GATT REFRESHED")
                        },{
                            BleLogHelper.writeLog("FAIL REFRESHING GATT")
                        })

                BleLogHelper.writeLog("Send Request Connection Command $deviceType")
                val command = BraincareBluetoothCommandProtocol.createRequestConnectionCommandFrame(deviceType)
                connection.writeCharacteristic(BraincareBluetoothProtocol.rxCharacteristicUUID, command)
            }
            .delay(300, TimeUnit.MILLISECONDS)
            .subscribe({
                BleLogHelper.writeLog("Connection Established ${device.type}")
                val iscon= this.isConnecting
                startBlinkingDeviceLed(deviceType, pairColor)
                connectionFlowListeners.forEach { it.onConnectionEstablished(device) }
            }, {
                BleLogHelper.writeError("Connection Lost ${device.type}", it)
                BleLogHelper.writeError("Retrying...", it)
                val iscon= this.isConnecting
                if (isMonitoring || isConnecting || deviceType == DeviceType.DONGLE){
                    connectionStateListeners.forEach {
                        if (deviceType == DeviceType.SENSOR) {
                            sensorNotificationSubscription?.dispose()
                            sensorRssiSubscription?.dispose()
                            blinkingDeviceLedsSubscription?.dispose()
                            disconnectFromDevice(DeviceType.SENSOR)
                        } else {
                            dongleRssiSubscription?.dispose()
                            disconnectFromDevice(DeviceType.DONGLE)
                        }
                        isConnecting = false
                        it.onConnectionLost(device)
                    }
                }else{
                    reconnectToDevice(device, pairColor)
                }
            })

    if (deviceType == DeviceType.SENSOR) {
        sensorConnectionSubscription = connectionSubscription
    } else {
        dongleConnectionSubscription = connectionSubscription
    }
}

在connection.writeCharacteristic(BraincareBluetoothProtocol.rxCharacteristicUUID,命令)之后立即触发异常

记录错误:

2019-05-21 10:54:11.816 11797-11889/io.b4c.brain4care.debug E/BLEBC: 21/05/2019 10:54:11.810 - Connection Lost SENSOR
com.polidea.rxandroidble2.exceptions.BleDisconnectedException: Disconnected from D4:57:4F:53:44:E7 with status 19 (UNKNOWN)
    at com.polidea.rxandroidble2.internal.connection.RxBleGattCallback$2.onConnectionStateChange(RxBleGattCallback.java:77)
    at android.bluetooth.BluetoothGatt$1$4.run(BluetoothGatt.java:268)
    at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:789)
    at android.bluetooth.BluetoothGatt.-wrap0(Unknown Source:0)
    at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:264)
    at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
    at android.os.Binder.execTransact(Binder.java:682)

1 个答案:

答案 0 :(得分:0)

status=19GATT_CONN_TERMINATE_PEER_USER。此版本自1.8.1起的库正确列出了原因。始终建议使用基本上任何库的最新版本,因为随着时间的推移它们会带来改进。

帖子中没有明确的问题。借助所提供的信息,您将无法提供以上所有信息-您使用的外围设备会与应用断开连接。发生这种情况的原因取决于您的外围设备,您可以在文档中找到答案。

请记住,较新的Android版本不允许同时对多个BLE设备同时进行配对过程。两个设备的配对应顺序进行,即

  1. 外围设备A开始配对
  2. 外围设备A完成配对
  3. 外围设备B开始配对
  4. 外围设备B完成配对

外围设备可以同时连接,但一次只能进行一次配对。