BLE implementation started
parent
38661621e3
commit
23ab0483e0
@ -0,0 +1,181 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:diplomaticquarterapp/theme/colors.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 TemperatureHomePage extends StatefulWidget {
|
||||
@override
|
||||
State<TemperatureHomePage> createState() => _TemperatureHomePageState();
|
||||
}
|
||||
|
||||
class _TemperatureHomePageState extends State<TemperatureHomePage> {
|
||||
String connectionStatus = "disconnected";
|
||||
String currentTempInCelsius = "0.0";
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AppScaffold(
|
||||
appBarTitle: TranslationBase.of(context).temperature,
|
||||
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: $currentTempInCelsius" + "\u2103"),
|
||||
],
|
||||
),
|
||||
);
|
||||
} 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...";
|
||||
});
|
||||
|
||||
FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false).then((value) {
|
||||
List<ScanResult> blueToothDevices = value;
|
||||
blueToothDevices.forEach((element) async {
|
||||
if (element.device.localName.isNotEmpty) {
|
||||
if (element.device.localName.toLowerCase() == "temp") {
|
||||
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) {
|
||||
List<BluetoothService> services = await element.device.discoverServices();
|
||||
services.forEach((service) {
|
||||
if (service.serviceUuid.toString().contains("1809")) {
|
||||
print(service.serviceUuid);
|
||||
service.characteristics.forEach((characteristic) async {
|
||||
if (characteristic.characteristicUuid.toString().toLowerCase().contains("2a1c")) {
|
||||
print(characteristic.characteristicUuid);
|
||||
characteristic.onValueReceived.listen((event) {
|
||||
print("onValueReceived Stream");
|
||||
print(event);
|
||||
setState(() {
|
||||
currentTempInCelsius = convertIntListToHex(event);
|
||||
});
|
||||
});
|
||||
await characteristic.setNotifyValue(true);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
await element.device.connect(timeout: Duration(seconds: 35));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
String convertIntListToHex(List<int> byteArray) {
|
||||
String b = (byteArray.map((x) {
|
||||
return x.toRadixString(16);
|
||||
})).join(";");
|
||||
List<String> hexString = b.split(";");
|
||||
print("HexString: $hexString");
|
||||
String returnString = hexString[3] + hexString[2] + hexString[1];
|
||||
print("Temp Hex String: $returnString");
|
||||
final number = int.parse(returnString, radix: 16);
|
||||
print("Temp Number: ${number * 0.01}");
|
||||
|
||||
return (number * 0.01).toStringAsFixed(1);
|
||||
}
|
||||
}
|
||||
|
||||
class BluetoothOffScreen extends StatelessWidget {
|
||||
const BluetoothOffScreen({Key key, this.adapterState}) : super(key: key);
|
||||
|
||||
final BluetoothAdapterState adapterState;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ScaffoldMessenger(
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.lightBlue,
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
const Icon(
|
||||
Icons.bluetooth_disabled,
|
||||
size: 200.0,
|
||||
color: Colors.white54,
|
||||
),
|
||||
Text(
|
||||
'Bluetooth Adapter is ${adapterState != null ? adapterState.toString().split(".").last + ", Please turn on your bluetooth to continue." : 'not available'}.',
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).primaryTextTheme.titleSmall?.copyWith(color: Colors.white),
|
||||
),
|
||||
if (Platform.isAndroid)
|
||||
ElevatedButton(
|
||||
child: const Text('TURN ON'),
|
||||
onPressed: () async {
|
||||
try {
|
||||
if (Platform.isAndroid) {
|
||||
await FlutterBluePlus.turnOn();
|
||||
}
|
||||
} catch (e) {}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue