diff --git a/code/src/Fitbot/app/src/main/java/com/example/fitbot/bluetooth/DeviceScanner.java b/code/src/Fitbot/app/src/main/java/com/example/fitbot/bluetooth/DeviceScanner.java index d06d33d..8873fc0 100644 --- a/code/src/Fitbot/app/src/main/java/com/example/fitbot/bluetooth/DeviceScanner.java +++ b/code/src/Fitbot/app/src/main/java/com/example/fitbot/bluetooth/DeviceScanner.java @@ -18,8 +18,8 @@ import java.nio.charset.StandardCharsets; import java.util.UUID; public class DeviceScanner { - private Context context; - private static final UUID CORRECT_CHARACTERISTIC_UUID = UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8"); + private Context context; + private static final UUID CORRECT_CHARACTERISTIC_UUID = UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8"); // ESP UUID characteristic private BluetoothAdapter bluetoothAdapter; private BluetoothLeScanner bluetoothLeScanner; private boolean scanning; @@ -36,7 +36,6 @@ public class DeviceScanner { public void scanLeDevice() { if (!scanning) { - // Stops scanning after a predefined scan period. handler.postDelayed(new Runnable() { @Override public void run() { @@ -45,7 +44,6 @@ public class DeviceScanner { Log.i("DeviceScanner", "Stopped scanning after scan period"); } }, SCAN_PERIOD); - scanning = true; bluetoothLeScanner.startScan(leScanCallback); Log.i("DeviceScanner", "Started scanning"); @@ -56,6 +54,7 @@ public class DeviceScanner { } } + // Stops scanning for devices. public void stopScan() { if (scanning) { scanning = false; @@ -64,7 +63,7 @@ public class DeviceScanner { } } - // Device scan callback. + // Device scan callback to find the ESP32 private ScanCallback leScanCallback = new ScanCallback() { @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() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { @@ -89,6 +89,7 @@ public class DeviceScanner { } } + // Discover services and characteristics @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { @@ -103,6 +104,7 @@ public class DeviceScanner { } } + // Read the characteristic @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { @@ -110,16 +112,19 @@ public class DeviceScanner { } } + // Characteristic changed @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.i("DeviceScanner", "Characteristic changed: " + new String(characteristic.getValue(), StandardCharsets.UTF_8)); } }; + // Connect to the ESP32 public void connectToDevice(BluetoothDevice device) { BluetoothGatt gatt = device.connectGatt(context, false, gattCallback); } + // Check if the characteristic has the correct UUID private boolean isCorrectCharacteristic(BluetoothGattCharacteristic characteristic) { // Log the UUID of the characteristic Log.i("DeviceScanner", String.valueOf(characteristic.getUuid()));