我正在尝试建立一个连接,如果连接一旦丢失,它将再次自动连接。再次建立连接时开始调试。
所以第一个连接很完美,我可以从 rxBleConnection 设置通知。但是当我关闭我的 ble 板时,我得到了 BleAlreadyConnectedException,除非我再次关闭应用程序,否则我无法连接。
fun connect(address: String) {
if (mRxBleClient == null || address == null) {
Log.e(TAG, "mRxBleClient not initialized or unspecified address.")
}
mRxDevice = mRxBleClient?.getBleDevice(address)
mRxDevice!!.observeConnectionStateChanges() // Observe changes to the ble connection
.subscribe(
{ connectionState ->
when (connectionState!!) {
RxBleConnection.RxBleConnectionState.CONNECTED -> {
Log.e(TAG, "Device connected")
broadcastUpdate(ACTION_GATT_CONNECTED)
}
RxBleConnection.RxBleConnectionState.CONNECTING -> {
Log.e(TAG, "Device connecting")
broadcastUpdate(ACTION_GATT_CONNECTING)
}
RxBleConnection.RxBleConnectionState.DISCONNECTING -> {
Log.e(TAG, "Device disconnecting")
broadcastUpdate(ACTION_GATT_DISCONNECTING)
}
RxBleConnection.RxBleConnectionState.DISCONNECTED -> {
Log.e(TAG, "Device disconnected")
broadcastUpdate(ACTION_GATT_DISCONNECTED)
}
}
},
{ throwable ->
// Handle an error here.
Log.e(TAG, "Connection state throwable " + throwable)
}
)
mDeviceAddress = address
createConnection()
}
fun createConnection() {
connectionSubscription = mRxDevice!!
.establishConnection(true)
.observeOn(AndroidSchedulers.mainThread())
.doOnUnsubscribe(this::clearSubscription)
.subscribe(
{
rxBleConnection ->
Log.e(TAG, "Connected")
mRxBleConnection = rxBleConnection
isCommissioning = true
setUpNotification() // When connected try to set up the notifications
},
{
throwable ->
Log.e(TAG, "Something went wrong when connecting " + throwable)
}
)
}
和通知
fun setUpNotification() {
Log.e(TAG, "setUpNotification")
mRxBleConnection!!.discoverServices().subscribe({ service ->
service.getService(Data_Message_UUID).forEach({ layer ->
for (character in layer.characteristics) {
setCharacteristicNotification(character)
}
})
})
}
所以第一个连接工作得很好。但是在我的电路板重新启动后,connectionObservable 给了我错误 BleAlreadyConnectedException status 8
我希望应用程序再次连接并再次为新连接设置通知。