Skip to content

Commit

Permalink
refs #25 Add the handling for de-dup in the BLE scan
Browse files Browse the repository at this point in the history
  • Loading branch information
truedat101 committed Mar 23, 2024
1 parent 2a11a51 commit 334e8fe
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 14 deletions.
28 changes: 25 additions & 3 deletions magnet_app/lib/app_state_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
// import 'package:location_permissions/location_permissions.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:cbl/cbl.dart';
import 'package:observable_ish/observable_ish.dart';


/**
Expand All @@ -28,6 +29,10 @@ class AppState extends BLEProvider {
bool timerStatus = false;
AsyncDatabase? _db;

RxList<Map> _devices = RxList<Map>(); /* = [
]; */

Timer? btnStatusTimer;


Expand Down Expand Up @@ -125,10 +130,20 @@ class AppState extends BLEProvider {
}


/*
device.serviceData.forEach((k, v) {
print('{ key: $k, value: $v }');
});

*/
// _genericBleDevices_connected.addIf(!_genericBleDevices_connected.contains(device),device);
// int sz = _devices.length;
_devices.addIf(!_devices.contains(device), {
'id': device.id,
'name': device.name,
'type': device.manufacturerData.toString()
});

// if (sz < _devices.length)
print(device.name +
" UUIDs: " +
device.serviceUuids.toString() +
Expand All @@ -138,11 +153,12 @@ class AppState extends BLEProvider {
device.id);
}
// print(device.id);
// XXX TODO: add a proper target name to match
if (device.name == "target") {
discoveredDevice = device;
foundDeviceWaitingToConnect = true;
notifyListeners();
print("Halo is found");
print("Device is found");

//Stop the scan
scanDeviceStream.cancel();
Expand All @@ -160,6 +176,7 @@ class AppState extends BLEProvider {
scanStarted = false;

if (!connected) {
print("Not connected");
// XXX TODO: update screen at end of scan
// _bleUpdateScreenState("disconnected");
}
Expand All @@ -180,15 +197,20 @@ class AppState extends BLEProvider {
_bleUpdateScreenState("disconnected");
}

RxList<Map> getDevices() {
return _devices;
}

void scanAndConnectToDevice() async {
//Update Screen
// XXX TODO: show progress on scan
// _bleUpdateScreenState("connecting");

startScan();

// XXX Why do we need this?
if (connected) {
print("isCalled");
print("scanAndConnectToDevice is connected");
} else {
print("Is not connected");
}
Expand Down
58 changes: 56 additions & 2 deletions magnet_app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import 'dart:async';

import 'package:flutter_neumorphic/flutter_neumorphic.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
Expand All @@ -11,6 +13,8 @@ import 'package:magnet_app/app_state_model.dart';
import 'package:magnet_app/provider/ble_provider.dart';
import 'package:audioplayers/audioplayers.dart';

// import 'package:horizontal_data_table/horizontal_data_table.dart';


Future<void> main() async {

Expand Down Expand Up @@ -60,10 +64,23 @@ class MyApp extends StatelessWidget {
}
}

class MyHomePage extends StatelessWidget {
final player = AudioPlayer();
class MyHomePage extends StatefulWidget {
MyHomePage({super.key});

@override
State<StatefulWidget> createState() => _MyHomePageWidgetState();
}

class _MyHomePageWidgetState extends State<MyHomePage> {
final player = AudioPlayer();

List<Map> _devicesScannedNow = [

];

@override
State<StatefulWidget> createState() => _MyHomePageWidgetState();

Widget build(BuildContext context) {
final appState = Provider.of<AppState>(context, listen: false);

Expand All @@ -72,6 +89,18 @@ class MyHomePage extends StatelessWidget {
child: Icon(Icons.add, size: 30),
onPressed: () {
player.play(AssetSource('audio/scanning_loop.wav'));
Timer timer;
int secs = 0;
timer = Timer.periodic(Duration(milliseconds:1000),(timer){
if (secs < 30) {
secs += 1;
_devicesScannedNow = appState.getDevices();
print("devices ${_devicesScannedNow}");

} else {
timer.cancel();
}
});
appState.scanAndConnectToDevice();
},
),
Expand Down Expand Up @@ -124,6 +153,7 @@ class MyHomePage extends StatelessWidget {
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[

/*
NeumorphicButton(
onPressed: () {
Expand Down Expand Up @@ -177,6 +207,7 @@ class MyHomePage extends StatelessWidget {
style: TextStyle(color: _textColor(context)),
)),
*/
_createDataTable()
],
),
),
Expand All @@ -199,6 +230,29 @@ class MyHomePage extends StatelessWidget {
return Colors.black;
}
}

DataTable _createDataTable() {
return DataTable(columns: _createColumns(), rows: _createRows());
}
List<DataColumn> _createColumns() {
return [
DataColumn(label: Text('ID')),
DataColumn(label: Text('Name')),
DataColumn(label: Text('Type'))
];
}

List<DataRow> _createRows() {
return _devicesScannedNow
.map((device) => DataRow(cells: [
DataCell(Text('#' + device['id'].toString())),
DataCell(Text(device['name'])),
DataCell(Text(device['type']))
]))
.toList();
}


}

Widget switchScreen(AppState app_provider) {
Expand Down
16 changes: 8 additions & 8 deletions magnet_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.1"
horizontal_data_table:
dependency: "direct main"
description:
name: horizontal_data_table
sha256: c8ab5256bbced698a729f3e0ff2cb0e8e97416cdbb082860370eaf883badf722
url: "https://pub.dev"
source: hosted
version: "4.3.1"
http:
dependency: transitive
description:
Expand Down Expand Up @@ -441,6 +433,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0"
observable_ish:
dependency: "direct main"
description:
name: observable_ish
sha256: d102a9dab48c332a81d99bcd0a59d1aa956dc6c147f6c0a1b197a74bddbf76e9
url: "https://pub.dev"
source: hosted
version: "4.0.8"
page_transition:
dependency: "direct main"
description:
Expand Down
3 changes: 2 additions & 1 deletion magnet_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ dependencies:
# For the Enterprise Edition add `cbl_flutter_ee` instead.
cbl_flutter_ce: ^2.2.2
iconic: ^0.0.3
horizontal_data_table: ^4.3.1
# horizontal_data_table: ^4.3.1
observable_ish: ^4.0.8
flutter_neumorphic:
git:
url: https://github.com/truedat101/Flutter-Neumorphic
Expand Down

0 comments on commit 334e8fe

Please sign in to comment.