1

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.

Erazx
  • 71
  • 1
  • 2
  • 7
  • What version of the library you use? You do not show the full code that is touching the connection. Could you also paste the logs from your application showing the issue with setting `RxBleLog.setLogLevel(RxBleLog.VERBOSE)`? – Dariusz Seweryn Jun 20 '17 at 10:04
  • @s_noopy i´m using the latest (v1.3.1) This is the full code for connecting, without the commissioning [link to pastebin](https://pastebin.com/D1jYT4AA) – Erazx Jun 20 '17 at 10:44
  • I´ll add the log level and test that soon – Erazx Jun 20 '17 at 10:47
  • I do not see any place in the code in which you call `connectionSubscription.unsubscribe()` – Dariusz Seweryn Jun 20 '17 at 10:48
  • @s_noopy I used to have that, but the problem was still that if I unsubscribe from connectionSubscription the device would not be found again and no connection established. – Erazx Jun 20 '17 at 11:02
  • Maybe i just didn´t know where to put it.. – Erazx Jun 20 '17 at 11:04
  • @s_noopy this is the log when turning of the board [Link to log](https://pastebin.com/UVVBWb8R), in the end i turned it back on, that´w when the gat state changes from 0 and 2. – Erazx Jun 20 '17 at 12:42
  • In the logs I do not see any `BleAlreadyConnectedException`. I can see only that the connection get a timeout and is closed. The gatt state is from a `BluetoothGattServer` if I can tell. – Dariusz Seweryn Jun 20 '17 at 13:26

0 Answers0