I´m trying to establish a connection that will auto connect again if the connection was once lost. With the start of a commissioning when connection is established again.
So the first connection works perfect, and I can set up notifications from the rxBleConnection. But when I turn off my ble board I get the BleAlreadyConnectedException and I´m not able to connect unless I close the app again.
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)
}
)
}
And the notifications
fun setUpNotification() {
Log.e(TAG, "setUpNotification")
mRxBleConnection!!.discoverServices().subscribe({ service ->
service.getService(Data_Message_UUID).forEach({ layer ->
for (character in layer.characteristics) {
setCharacteristicNotification(character)
}
})
})
}
So the first connection works just fine. But after a restart of my board the connectionObservable gives me the error BleAlreadyConnectedException status 8
I want the app to connect again and set up the notifications once again for the new connection.