ฉันกำลังพัฒนาแอปพลิเคชันเพื่อจับคู่กับเซ็นเซอร์สองประเภทกับแอป และเซ็นเซอร์เหล่านี้ก็จับคู่กันด้วย เราได้พัฒนาโปรโตคอลการสื่อสารบลูทูธแบบกำหนดเองของเรา โดยปกติแล้วการเชื่อมต่อจะทำงานได้ดี แต่ก็ยังมีข้อผิดพลาดบางอย่างที่ฉันแก้ไขได้ยาก
การแยกเซ็นเซอร์ 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
}
}
ข้อยกเว้นเริ่มทำงานหลังจากการเชื่อมต่อ 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)