NordicSemiconductor / Android-BLE-Library

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.
BSD 3-Clause "New" or "Revised" License
2k stars 414 forks source link

Add attachClientConnection method as a server only alternative to connect #450

Closed CyDragon80 closed 1 year ago

CyDragon80 commented 1 year ago

This is based in part on work from this PR https://github.com/NordicSemiconductor/Android-BLE-Library/pull/231 which references this issue https://github.com/NordicSemiconductor/Android-BLE-Library/issues/229 I have endeavored to cover this use case with the minimum possible code disruption. The current behavior should be exactly the same for code using the connection back to the client. I have only added one method "attachClientConnection" which can be used as a server-only alternative to using "connect" in "onDeviceConnectedToServer". Example use shown below based on a Java version of the server example in this repo (ServerConnection extends BleManager).

@Override
public void onDeviceConnectedToServer(BluetoothDevice device) {
    String address = device.getAddress();
    Log.d(TAG, "Device connected " + address);
    ServerConnection newConnection = new ServerConnection(context);
    serverConnections.put(address, newConnection);
    newConnection.useServer(this);
    // either setup as server only or dual connection
    newConnection.attachClientConnection(device); // sets up connection server only
    //newConnection.connect(device).enqueue(); // queues connection back to client for dual connection
}
CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.

philips77 commented 1 year ago

Hello, That's a quite good idea. Thank you for the PR. I'm starting my holidays today, but will come back to you next year. Merry Christmas!

CyDragon80 commented 1 year ago

Thanks for taking the PR. Any chance of this making it into a 2.6.0 alpha build in the near future? I have a scenario where a particular third party app I need to support simply refuses to work with me if I attempt a back connection to it. So far I've had to run tests with my temporary local copy of the library to get anywhere with it.

philips77 commented 1 year ago

I'm releasing it now. Just need to write some documentation here and there.

philips77 commented 1 year ago

2.6.0 final should be available soon. I just pushed it.