Skip to content

Commit

Permalink
refactor: add stream based methods in BluetoothService to handle bl…
Browse files Browse the repository at this point in the history
…uetooth operations
  • Loading branch information
mediocre9 committed Jan 6, 2024
1 parent cad9d70 commit 9f5d41b
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions lib/services/bluetooth_service.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';

abstract interface class IBluetoothService {
Stream<BluetoothDiscoveryResult> startDiscovery();
Future<bool?> isEnabled();

Future<void> cancelDiscovery();

Future<List<BluetoothDevice>> fetchPairedDevices();
Future<bool?> enableBluetoothAdapter();

Future<bool?> enableBluetooth();
Stream<BluetoothState> onAdapterStateChanges();

Future<bool?> isEnabled();
Future<List<BluetoothDevice>> getPairedDevices();

Stream<BluetoothDiscoveryResult> startDiscovery();

Future<bool?> bondDevice(BluetoothDevice device);

Future<void> cancelDiscovery();
Future<BluetoothConnection> establishConnectionTo(BluetoothDevice device);
}

class BluetoothService implements IBluetoothService {
Expand All @@ -30,22 +34,32 @@ class BluetoothService implements IBluetoothService {
}

@override
Future<List<BluetoothDevice>> fetchPairedDevices() async {
Future<List<BluetoothDevice>> getPairedDevices() async {
return await _bluetooth.getBondedDevices();
}

@override
Future<bool?> bondDevice(BluetoothDevice device) async {
return device.isBonded ? true : await _bluetooth.bondDeviceAtAddress(device.address);
}

@override
Future<bool?> enableBluetooth() async {
Future<bool?> enableBluetoothAdapter() async {
return await _bluetooth.requestEnable();
}

@override
Future<bool?> isEnabled() {
return _bluetooth.isEnabled;
}

@override
Stream<BluetoothState> onAdapterStateChanges() {
return _bluetooth.onStateChanged();
}

@override
Future<bool?> bondDevice(BluetoothDevice device) async {
return await _bluetooth.bondDeviceAtAddress(device.address);
}

@override
Future<BluetoothConnection> establishConnectionTo(BluetoothDevice device) {
return BluetoothConnection.toAddress(device.address);
}
}

0 comments on commit 9f5d41b

Please sign in to comment.