Skip to content

Commit b7ad481

Browse files
committed
Refactor BluetoothCentral and include method to get bond state
1 parent a5b7ef4 commit b7ad481

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

app/src/main/java/com/welie/blessedexample/BluetoothHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.welie.blessed.GattStatus;
1818
import com.welie.blessed.HciStatus;
1919

20+
import com.welie.blessed.PhyOptions;
21+
import com.welie.blessed.PhyType;
2022
import com.welie.blessed.ScanFailure;
2123
import com.welie.blessed.WriteType;
2224

@@ -117,10 +119,14 @@ public void onServicesDiscovered(@NotNull BluetoothPeripheral peripheral) {
117119
// Request a new connection priority
118120
peripheral.requestConnectionPriority(ConnectionPriority.HIGH);
119121

122+
peripheral.setPreferredPhy(PhyType.LE_2M, PhyType.LE_2M, PhyOptions.S2);
123+
120124
// Read manufacturer and model number from the Device Information Service
121125
peripheral.readCharacteristic(DIS_SERVICE_UUID, MANUFACTURER_NAME_CHARACTERISTIC_UUID);
122126
peripheral.readCharacteristic(DIS_SERVICE_UUID, MODEL_NUMBER_CHARACTERISTIC_UUID);
123127

128+
peripheral.readPhy();
129+
124130
// Turn on notifications for Current Time Service and write it if possible
125131
BluetoothGattCharacteristic currentTimeCharacteristic = peripheral.getCharacteristic(CTS_SERVICE_UUID, CURRENT_TIME_CHARACTERISTIC_UUID);
126132
if (currentTimeCharacteristic != null) {
@@ -376,7 +382,7 @@ private void startScan() {
376382
handler.postDelayed(new Runnable() {
377383
@Override
378384
public void run() {
379-
central.scanForPeripheralsWithServices(new UUID[]{BLP_SERVICE_UUID, HTS_SERVICE_UUID, HRS_SERVICE_UUID, PLX_SERVICE_UUID, WSS_SERVICE_UUID, GLUCOSE_SERVICE_UUID});
385+
central.scanForPeripheralsWithServices(new UUID[]{BLP_SERVICE_UUID, HTS_SERVICE_UUID, PLX_SERVICE_UUID, WSS_SERVICE_UUID, GLUCOSE_SERVICE_UUID});
380386
}
381387
},1000);
382388
}

blessed/src/main/java/com/welie/blessed/BluetoothCentral.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,37 @@
2323

2424
package com.welie.blessed;
2525

26+
import android.annotation.SuppressLint;
27+
import android.bluetooth.BluetoothDevice;
28+
2629
import org.jetbrains.annotations.NotNull;
27-
import org.jetbrains.annotations.Nullable;
2830

2931
import java.util.Objects;
3032

3133
/**
3234
* This class represent a remote Central
3335
*/
36+
@SuppressLint("MissingPermission")
3437
public class BluetoothCentral {
3538

36-
@NotNull
37-
private final String address;
38-
39-
@Nullable
40-
private final String name;
39+
@NotNull private final BluetoothDevice device;
4140

4241
private int currentMtu = 23;
4342

44-
BluetoothCentral(@NotNull String address, @Nullable String name) {
45-
this.address = Objects.requireNonNull(address, "address is null");
46-
this.name = name;
43+
BluetoothCentral(@NotNull BluetoothDevice device) {
44+
this.device = device;
4745
}
4846

4947
public @NotNull String getAddress() {
50-
return address;
48+
return device.getAddress();
5149
}
5250

5351
public @NotNull String getName() {
54-
return name == null ? "" : name;
52+
return device.getName() == null ? "" : device.getName();
53+
}
54+
55+
public BondState getBondState() {
56+
return BondState.fromValue(device.getBondState());
5557
}
5658

5759
protected void setCurrentMtu(final int currentMtu) {

blessed/src/main/java/com/welie/blessed/BluetoothPeripheralManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public class BluetoothPeripheralManager {
103103
protected final BluetoothGattServerCallback bluetoothGattServerCallback = new BluetoothGattServerCallback() {
104104
@Override
105105
public void onConnectionStateChange(final BluetoothDevice device, final int status, final int newState) {
106-
107106
if (status == BluetoothGatt.GATT_SUCCESS) {
108107
if (newState == BluetoothProfile.STATE_CONNECTED) {
109108
// Call connect() even though we are already connected
@@ -132,7 +131,7 @@ public void onConnectionStateChange(final BluetoothDevice device, final int stat
132131

133132
private void handleDeviceConnected(@NotNull final BluetoothDevice device) {
134133
Logger.i(TAG,"Central '%s' (%s) connected", device.getName(), device.getAddress());
135-
final BluetoothCentral bluetoothCentral = new BluetoothCentral(device.getAddress(), device.getName());
134+
final BluetoothCentral bluetoothCentral = new BluetoothCentral(device);
136135
connectedCentralsMap.put(bluetoothCentral.getAddress(), bluetoothCentral);
137136
mainHandler.post(new Runnable() {
138137
@Override
@@ -757,7 +756,7 @@ private BluetoothCentral getCentral(@NotNull final BluetoothDevice device) {
757756

758757
BluetoothCentral result = connectedCentralsMap.get(device.getAddress());
759758
if (result == null) {
760-
result = new BluetoothCentral(device.getAddress(), device.getName());
759+
result = new BluetoothCentral(device);
761760
}
762761
return result;
763762
}

0 commit comments

Comments
 (0)