Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions ble/src/main/java/no/nordicsemi/android/ble/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,14 @@ public final DisconnectRequest disconnect() {
return Request.disconnect().setRequestHandler(requestHandler);
}

/**
* "Server only" alternative to using connect() in onDeviceConnectedToServer.
* This simply associates the connection to the passed client.
*/
public void attachClientConnection(BluetoothDevice client) {
requestHandler.attachClientConnection(client);
}

/**
* Returns a request to create bond with the device. The device must be first set using
* {@link #connect(BluetoothDevice)} which will try to connect to the device.
Expand Down
66 changes: 44 additions & 22 deletions ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,49 @@ void useServer(@Nullable final BleServerManager server) {
this.serverManager = server;
}

/**
* If doing a server-only connection, use this instead of {@link BleManager#connect(BluetoothDevice)}
* inside of your onDeviceConnectedToServer handler.
*/
void attachClientConnection(BluetoothDevice clientDevice) {
// should either setup as server only (this method) or two way connection (connect method), not both
if (this.bluetoothDevice != null) {
log(Log.ERROR, () -> "attachClientConnection called on existing connection, call ignored");
} else {
this.bluetoothDevice = clientDevice;
// If using two way connection via connect(), the server attributes would be setup after discovery.
// Since we are opting to use server only connection, we must do this here instead.
initializeServerAttributes();
// the other path also calls this part of the callbacks
initialize();
}
}

private void initializeServerAttributes() {
if (serverManager != null) {
final BluetoothGattServer server = serverManager.getServer();
if (server != null) {
for (final BluetoothGattService service: server.getServices()) {
for (final BluetoothGattCharacteristic characteristic: service.getCharacteristics()) {
if (!serverManager.isShared(characteristic)) {
if (characteristicValues == null)
characteristicValues = new HashMap<>();
characteristicValues.put(characteristic, characteristic.getValue());
}
for (final BluetoothGattDescriptor descriptor: characteristic.getDescriptors()) {
if (!serverManager.isShared(descriptor)) {
if (descriptorValues == null)
descriptorValues = new HashMap<>();
descriptorValues.put(descriptor, descriptor.getValue());
}
}
}
}
onServerReady(server);
}
}
}

/**
* Closes and releases resources.
*/
Expand Down Expand Up @@ -2156,28 +2199,7 @@ public void onServicesDiscovered(@NonNull final BluetoothGatt gatt, final int st
postCallback(c -> c.onServicesDiscovered(gatt.getDevice(), optionalServicesFound));

// Initialize server attributes.
if (serverManager != null) {
final BluetoothGattServer server = serverManager.getServer();
if (server != null) {
for (final BluetoothGattService service: server.getServices()) {
for (final BluetoothGattCharacteristic characteristic: service.getCharacteristics()) {
if (!serverManager.isShared(characteristic)) {
if (characteristicValues == null)
characteristicValues = new HashMap<>();
characteristicValues.put(characteristic, characteristic.getValue());
}
for (final BluetoothGattDescriptor descriptor: characteristic.getDescriptors()) {
if (!serverManager.isShared(descriptor)) {
if (descriptorValues == null)
descriptorValues = new HashMap<>();
descriptorValues.put(descriptor, descriptor.getValue());
}
}
}
}
onServerReady(server);
}
}
initializeServerAttributes();

// Obtain the queue of initialization requests.
// First, let's call the deprecated initGatt(...).
Expand Down