Skip to content

Commit

Permalink
Potentially updated to new FBP API
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsRefsgaard committed Aug 3, 2023
1 parent adfc16c commit 4f59c0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
44 changes: 30 additions & 14 deletions lib/ui/pages/devices_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ class DevicesPage extends StatefulWidget {
class DevicesPageState extends State<DevicesPage> {
int selected = 40;
BluetoothDevice? selectedDevice;
FlutterBluePlus flutterBlue = FlutterBluePlus.instance;

StreamSubscription? isScanningStream;
StreamSubscription? scanResultStream;
StreamSubscription? bluetoothStateStream;

@override
void dispose() {
flutterBlue.stopScan();
FlutterBluePlus.stopScan();
isScanningStream?.cancel();
scanResultStream?.cancel();
bluetoothStateStream?.cancel();
Expand Down Expand Up @@ -285,29 +284,46 @@ class DevicesPageState extends State<DevicesPage> {
),
onTap: () async {
if (device.status != DeviceStatus.connected) {
// check adapter availability
if (await FlutterBluePlus.isAvailable == false) {
print("Bluetooth not supported by this device");
return;
}

// turn on bluetooth ourself if we can
if (Platform.isAndroid) {
await FlutterBluePlus.turnOn();
}

// wait bluetooth to be on
await FlutterBluePlus.adapterState
.where((s) => s == BluetoothAdapterState.on)
.first;

// start scanning for BTLE devices
bool isScanning = false;

isScanningStream = flutterBlue.isScanning.listen(
isScanningStream = FlutterBluePlus.isScanning.listen(
(scanBool) {
isScanning = scanBool;
},
);
bluetoothStateStream = flutterBlue.state.listen((state) {
if (state == BluetoothState.on && !isScanning) {
flutterBlue.startScan();
bluetoothStateStream =
FlutterBluePlus.adapterState.listen((state) {
if (state == BluetoothAdapterState.on && !isScanning) {
FlutterBluePlus.startScan();
isScanning = true;
} else {
// instantly start and stop a scan to turn on the BT adapter
flutterBlue.startScan();
flutterBlue.stopScan();
FlutterBluePlus.startScan();
FlutterBluePlus.stopScan();
}
});

await showConnectionDialog(context, CurrentStep.scan,
device, setState, selected, selectedDevice);

flutterBlue.stopScan();
FlutterBluePlus.stopScan();
}
}),
],
Expand Down Expand Up @@ -414,14 +430,14 @@ class DevicesPageState extends State<DevicesPage> {
),
Expanded(
child: StreamBuilder<List<ScanResult>>(
stream: flutterBlue.scanResults,
stream: FlutterBluePlus.scanResults,
initialData: const [],
builder: (context, snapshot) =>
SingleChildScrollView(
child: Column(
children: snapshot.data!
.where((element) =>
element.device.name.isNotEmpty)
element.device.localName.isNotEmpty)
.toList()
.asMap()
.entries
Expand All @@ -430,7 +446,7 @@ class DevicesPageState extends State<DevicesPage> {
selected:
bluetoothDevice.key == selected,
title: Text(bluetoothDevice
.value.device.name),
.value.device.localName),
selectedTileColor: Theme.of(context)
.primaryColor
.withOpacity(0.2),
Expand Down Expand Up @@ -516,7 +532,7 @@ class DevicesPageState extends State<DevicesPage> {
.translate("pages.devices.connection.done")
.toUpperCase()),
onPressed: () {
flutterBlue.stopScan();
FlutterBluePlus.stopScan();
if (selectedDevice != null) {
bloc.connectToDevice(
selectedDevice!,
Expand Down Expand Up @@ -585,7 +601,7 @@ class DevicesPageState extends State<DevicesPage> {
width: MediaQuery.of(context).size.height * 0.2,
height: MediaQuery.of(context).size.height * 0.2),
Text(
("${locale.translate("pages.devices.connection.step.confirm.1")} '${device?.name}' ${locale.translate("pages.devices.connection.step.confirm.2")}")
("${locale.translate("pages.devices.connection.step.confirm.1")} '${device?.localName}' ${locale.translate("pages.devices.connection.step.confirm.2")}")
.trim(),
style: aboutCardContentStyle,
textAlign: TextAlign.justify,
Expand Down
12 changes: 2 additions & 10 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,10 @@ packages:
dependency: "direct main"
description:
name: flutter_blue_plus
sha256: a977e90b7f9dcece90a95a287f8843e3b9028a61199c41bc279832c096d31d43
sha256: "0634a23f973848427c3bbe55955670fb3d0e534662f30e056fc9ec12abe4c718"
url: "https://pub.dev"
source: hosted
version: "1.5.2"
version: "1.12.9"
flutter_inappwebview:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1223,14 +1223,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.2.4"
protobuf:
dependency: transitive
description:
name: protobuf
sha256: "01dd9bd0fa02548bf2ceee13545d4a0ec6046459d847b6b061d8a27237108a08"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
provider:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies:
path_provider: ^2.0.2
json_annotation: ^4.4.0
app_settings: ^4.1.5
flutter_blue_plus: ^1.1.2
flutter_blue_plus: ^1.12.2
esense_flutter: ^0.6.2
open_settings: ^2.0.0

Expand Down

0 comments on commit 4f59c0a

Please sign in to comment.