feat: Update DeviceScanner to include ESP UUID characteristic

The DeviceScanner class in the Fitbot app is updated to include the ESP UUID characteristic for Bluetooth communication. This change adds the UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" to the CORRECT_CHARACTERISTIC_UUID constant. The purpose of this change is to enable communication with ESP32 devices.
This commit is contained in:
SebasKoedam
2024-05-31 11:14:22 +02:00
parent 49f97b57dd
commit bead6a5a13

View File

@@ -19,7 +19,7 @@ import java.util.UUID;
public class DeviceScanner { public class DeviceScanner {
private Context context; private Context context;
private static final UUID CORRECT_CHARACTERISTIC_UUID = UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8"); private static final UUID CORRECT_CHARACTERISTIC_UUID = UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8"); // ESP UUID characteristic
private BluetoothAdapter bluetoothAdapter; private BluetoothAdapter bluetoothAdapter;
private BluetoothLeScanner bluetoothLeScanner; private BluetoothLeScanner bluetoothLeScanner;
private boolean scanning; private boolean scanning;
@@ -36,7 +36,6 @@ public class DeviceScanner {
public void scanLeDevice() { public void scanLeDevice() {
if (!scanning) { if (!scanning) {
// Stops scanning after a predefined scan period.
handler.postDelayed(new Runnable() { handler.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
@@ -45,7 +44,6 @@ public class DeviceScanner {
Log.i("DeviceScanner", "Stopped scanning after scan period"); Log.i("DeviceScanner", "Stopped scanning after scan period");
} }
}, SCAN_PERIOD); }, SCAN_PERIOD);
scanning = true; scanning = true;
bluetoothLeScanner.startScan(leScanCallback); bluetoothLeScanner.startScan(leScanCallback);
Log.i("DeviceScanner", "Started scanning"); Log.i("DeviceScanner", "Started scanning");
@@ -56,6 +54,7 @@ public class DeviceScanner {
} }
} }
// Stops scanning for devices.
public void stopScan() { public void stopScan() {
if (scanning) { if (scanning) {
scanning = false; scanning = false;
@@ -64,7 +63,7 @@ public class DeviceScanner {
} }
} }
// Device scan callback. // Device scan callback to find the ESP32
private ScanCallback leScanCallback = private ScanCallback leScanCallback =
new ScanCallback() { new ScanCallback() {
@Override @Override
@@ -78,6 +77,7 @@ public class DeviceScanner {
}; };
}; };
// GATT callback to connect to the ESP32 and read the characteristic
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
@@ -89,6 +89,7 @@ public class DeviceScanner {
} }
} }
// Discover services and characteristics
@Override @Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) { public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) { if (status == BluetoothGatt.GATT_SUCCESS) {
@@ -103,6 +104,7 @@ public class DeviceScanner {
} }
} }
// Read the characteristic
@Override @Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) { if (status == BluetoothGatt.GATT_SUCCESS) {
@@ -110,16 +112,19 @@ public class DeviceScanner {
} }
} }
// Characteristic changed
@Override @Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.i("DeviceScanner", "Characteristic changed: " + new String(characteristic.getValue(), StandardCharsets.UTF_8)); Log.i("DeviceScanner", "Characteristic changed: " + new String(characteristic.getValue(), StandardCharsets.UTF_8));
} }
}; };
// Connect to the ESP32
public void connectToDevice(BluetoothDevice device) { public void connectToDevice(BluetoothDevice device) {
BluetoothGatt gatt = device.connectGatt(context, false, gattCallback); BluetoothGatt gatt = device.connectGatt(context, false, gattCallback);
} }
// Check if the characteristic has the correct UUID
private boolean isCorrectCharacteristic(BluetoothGattCharacteristic characteristic) { private boolean isCorrectCharacteristic(BluetoothGattCharacteristic characteristic) {
// Log the UUID of the characteristic // Log the UUID of the characteristic
Log.i("DeviceScanner", String.valueOf(characteristic.getUuid())); Log.i("DeviceScanner", String.valueOf(characteristic.getUuid()));