Weight Scale BLE
parent
3f767f738a
commit
5b7cde43d1
@ -0,0 +1,142 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:diplomaticquarterapp/theme/colors.dart';
|
||||||
|
import 'package:diplomaticquarterapp/uitl/ble_utils.dart';
|
||||||
|
import 'package:diplomaticquarterapp/uitl/bluetooth_off.dart';
|
||||||
|
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
|
||||||
|
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
|
||||||
|
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||||
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
|
||||||
|
class WeightScaleBLE extends StatefulWidget {
|
||||||
|
@override
|
||||||
|
State<WeightScaleBLE> createState() => _WeightScaleBLEState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _WeightScaleBLEState extends State<WeightScaleBLE> {
|
||||||
|
String connectionStatus = "disconnected";
|
||||||
|
String currentBloodGlucose = "0.0";
|
||||||
|
|
||||||
|
BluetoothDevice currentConnectedDevice;
|
||||||
|
|
||||||
|
StreamSubscription bleDevicesStream;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return AppScaffold(
|
||||||
|
appBarTitle: TranslationBase.of(context).weight,
|
||||||
|
showNewAppBar: true,
|
||||||
|
isShowDecPage: true,
|
||||||
|
showNewAppBarTitle: true,
|
||||||
|
backgroundColor: Color(0xffF8F8F8),
|
||||||
|
body: SingleChildScrollView(
|
||||||
|
child: StreamBuilder<BluetoothAdapterState>(
|
||||||
|
stream: FlutterBluePlus.adapterState,
|
||||||
|
initialData: BluetoothAdapterState.unknown,
|
||||||
|
builder: (c, snapshot) {
|
||||||
|
final adapterState = snapshot.data;
|
||||||
|
if (adapterState == BluetoothAdapterState.on) {
|
||||||
|
return Container(
|
||||||
|
margin: EdgeInsets.only(top: 200.0, left: 50.0, right: 50.0),
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
Center(
|
||||||
|
child: DefaultButton(
|
||||||
|
TranslationBase.of(context).start.toUpperCase(),
|
||||||
|
() {
|
||||||
|
checkBLEPermissions();
|
||||||
|
},
|
||||||
|
color: CustomColors.green,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(
|
||||||
|
height: 50.0,
|
||||||
|
),
|
||||||
|
Text("Connection state: $connectionStatus"),
|
||||||
|
SizedBox(
|
||||||
|
height: 50.0,
|
||||||
|
),
|
||||||
|
Text("Current Temp: $currentBloodGlucose" + " mg/dL"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
FlutterBluePlus.stopScan();
|
||||||
|
return SizedBox(height: 300.0, child: BluetoothOffScreen(adapterState: adapterState));
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkBLEPermissions() {
|
||||||
|
[Permission.location, Permission.storage, Permission.bluetooth, Permission.bluetoothConnect, Permission.bluetoothScan].request().then((status) {
|
||||||
|
startBLEConnection();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void startBLEConnection() {
|
||||||
|
if (FlutterBluePlus.isScanningNow == false) {
|
||||||
|
setState(() {
|
||||||
|
connectionStatus = "Connecting...";
|
||||||
|
});
|
||||||
|
|
||||||
|
bleDevicesStream = FlutterBluePlus.scanResults.listen(
|
||||||
|
(results) {
|
||||||
|
List<ScanResult> blueToothDevices = results;
|
||||||
|
blueToothDevices.forEach((element) async {
|
||||||
|
if (element.device.localName.isNotEmpty) {
|
||||||
|
if (element.device.localName.toLowerCase() == "sdic") {
|
||||||
|
element.device.connectionState.listen((BluetoothConnectionState state) async {
|
||||||
|
setState(() {
|
||||||
|
connectionStatus = state.toString();
|
||||||
|
});
|
||||||
|
if (state == BluetoothConnectionState.disconnected) {
|
||||||
|
// typically, start a periodic timer that tries to periodically reconnect.
|
||||||
|
// Note: you must always re-discover services after disconnection!
|
||||||
|
}
|
||||||
|
if (state == BluetoothConnectionState.connected) {
|
||||||
|
currentConnectedDevice = element.device;
|
||||||
|
bleDevicesStream.cancel();
|
||||||
|
List<BluetoothService> services = await element.device.discoverServices();
|
||||||
|
services.forEach((service) {
|
||||||
|
if (service.serviceUuid.toString().toLowerCase() == BLEUtils.WEIGHT_MEASUREMENT_SERVICE) {
|
||||||
|
print(service.serviceUuid);
|
||||||
|
service.characteristics.forEach((characteristic) async {
|
||||||
|
// if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF3) {
|
||||||
|
// print(characteristic.characteristicUuid);
|
||||||
|
// characteristic.onValueReceived.listen((event) {
|
||||||
|
// print("onValueReceived fff3 Stream");
|
||||||
|
// print(event);
|
||||||
|
// });
|
||||||
|
// await characteristic.setNotifyValue(true);
|
||||||
|
// }
|
||||||
|
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF1) {
|
||||||
|
print(characteristic.characteristicUuid);
|
||||||
|
characteristic.onValueReceived.listen((event) {
|
||||||
|
print("onValueReceived fff1 Stream");
|
||||||
|
print(event);
|
||||||
|
});
|
||||||
|
await characteristic.setNotifyValue(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await element.device.connect(timeout: Duration(seconds: 35));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
// onError(e) => print(e);
|
||||||
|
);
|
||||||
|
|
||||||
|
FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue