Skip to content

Commit

Permalink
Merge pull request #62 from inovait/ble_transport
Browse files Browse the repository at this point in the history
add ability to force bluetooth device's transport type
  • Loading branch information
matejdro authored Sep 26, 2023
2 parents f9745da + 2832eab commit 5a2c6de
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
This changelog is for older versions. See [Github Releases](https://github.com/inovait/neatle/releases) for newer versions.

## Version 0.10.0
- New: Added setTransport() to Connection
- New: Added setTransport() to ConnectionMonitor

## Version 0.9.3
- Fixed: #21 - BluetoothGattCharacteristic needed to extract value on subscription

Expand Down
17 changes: 15 additions & 2 deletions neatle/src/main/java/si/inova/neatle/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class Device implements Connection {
private boolean serviceDiscovered;
private BluetoothGatt gatt;

private int transport = 0;

private final CopyOnWriteArrayList<ConnectionHandler> connectionHandlers = new CopyOnWriteArrayList<>();
private final HashMap<UUID, CopyOnWriteArrayList<CharacteristicsChangedListener>> changeListeners = new HashMap<>();
private final CopyOnWriteArrayList<ConnectionStateListener> connectionStateListeners = new CopyOnWriteArrayList<>();
Expand Down Expand Up @@ -431,7 +433,13 @@ void connectWithGatt() {
}

NeatleLogger.d("Connecting with " + device.getName() + "[" + device.getAddress() + "]");
BluetoothGatt gatt = device.connectGatt(context, false, callback);
BluetoothGatt gatt;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
gatt = device.connectGatt(context, false, callback, transport);
} else {
gatt = device.connectGatt(context, false, callback);
}

synchronized (lock) {
if (state == BluetoothGatt.STATE_DISCONNECTED) {
Expand Down Expand Up @@ -545,7 +553,12 @@ public boolean isConnected() {
}
}

/*public void yield(BluetoothGattCallback from, BluetoothGattCallback to) {
@Override
public void setTransport(int transport) {
this.transport = transport;
}

/*public void yield(BluetoothGattCallback from, BluetoothGattCallback to) {
synchronized (lock) {
if (currentCallback == from) {
queue.add(0, currentCallback);
Expand Down
12 changes: 12 additions & 0 deletions neatle/src/main/java/si/inova/neatle/monitor/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,16 @@ public interface Connection {
void addServicesDiscoveredListener(ServicesDiscoveredListener listener);

void removeServicesDiscoveredListener(ServicesDiscoveredListener listener);

/**
* Set which transport will be used to connect to this device.
* Defaults to {@link BluetoothDevice#TRANSPORT_AUTO}.
*
* Only works on API Level 23 and higher.
*
* @param transport Either {@link BluetoothDevice#TRANSPORT_AUTO},
* {@link BluetoothDevice#TRANSPORT_BREDR} or
* {@link BluetoothDevice#TRANSPORT_LE}
*/
public void setTransport(int transport);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ public interface ConnectionMonitor {
*/
void setOnServiceDiscoveredListener(ServicesDiscoveredListener serviceDiscoveredListener);

/**
* Set which transport will be used to connect to the device under this monitor.
* Defaults to {@link BluetoothDevice#TRANSPORT_AUTO}.
*
* Only works on API Level 23 and higher.
*
* @param transport Either {@link BluetoothDevice#TRANSPORT_AUTO},
* {@link BluetoothDevice#TRANSPORT_BREDR} or
* {@link BluetoothDevice#TRANSPORT_LE}
*/
public void setTransport(int transport);

/**
* Starts this connection monitor, if it's not already running. If keepAlive is set this will
* also initiate a connection request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class ConnectionMonitorImpl implements ConnectionMonitor {
private final ConnHandler connectionHandler = new ConnHandler();
private final ReconnectRunnable reconnectRunnable = new ReconnectRunnable();

private int transport = 0;

private final IntentFilter btFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
private final BroadcastReceiver btReceiver = new BroadcastReceiver() {
@Override
Expand Down Expand Up @@ -105,6 +107,7 @@ public void start() {
connection.addConnectionHandler(connectionHandler);
connection.addConnectionStateListener(connectionHandler);
connection.addServicesDiscoveredListener(connectionHandler);
connection.setTransport(transport);

if (keepAlive) {
connection.connect();
Expand Down Expand Up @@ -137,6 +140,15 @@ public Connection getConnection() {
return connection;
}

@Override
public void setTransport(int transport) {
this.transport = transport;

if (this.connection != null) {
this.connection.setTransport(transport);
}
}

private void onBluetoothTurnedOn() {
if (keepAlive && connection != null) {
connection.connect();
Expand Down

0 comments on commit 5a2c6de

Please sign in to comment.