From dc20c8c1716da874ef1803f5f3837f4c352f2d2f Mon Sep 17 00:00:00 2001 From: YamiDoesDev Date: Sat, 7 Jan 2023 16:38:23 +0100 Subject: [PATCH] improved ui and code warnings --- lib/objects/bluetooth_object.dart | 8 ++--- lib/screens/bluetooth_device_settings.dart | 42 ++++++++++------------ lib/screens/bluetooth_screen.dart | 24 +++++++++---- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/lib/objects/bluetooth_object.dart b/lib/objects/bluetooth_object.dart index 5e12759..ccd2693 100644 --- a/lib/objects/bluetooth_object.dart +++ b/lib/objects/bluetooth_object.dart @@ -16,15 +16,15 @@ class BluetoothObject { String id = ""; String _primaryThumbprint = ""; - String _secondaryThumbprint = ""; + final String _secondaryThumbprint = ""; late BluetoothConnection? _connection; late Stream _connectionStream; late StreamSubscription _connectionStreamSubscription; - CloudServiceAPI _cloudServiceAPI = CloudServiceAPI(); + final CloudServiceAPI _cloudServiceAPI = CloudServiceAPI(); - late Uint8List _messageBufferBits; + //late Uint8List _messageBufferBits; late String _messageBufferChars = ""; bool _isDisconnecting = false; @@ -222,8 +222,6 @@ class BluetoothObject { Future sendData(String output) async { if (_connection == null) return; bool nameAvailable = await _cloudServiceAPI.checkNameAvailability(output); - // String output = "0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";// !§%&()=?#!?"; - // String output = "myDevice123";// !§%&()=?#!?"; if (!nameAvailable) return; id = output; _connection!.output.add(Uint8List.fromList(const AsciiEncoder().convert("$output \r\n"))); diff --git a/lib/screens/bluetooth_device_settings.dart b/lib/screens/bluetooth_device_settings.dart index 1c0ba49..6306d64 100644 --- a/lib/screens/bluetooth_device_settings.dart +++ b/lib/screens/bluetooth_device_settings.dart @@ -14,8 +14,7 @@ class BluetoothDeviceSettings extends StatefulWidget { class BluetoothDeviceSettingsState extends State { late BluetoothObject _bluetoothObject; - - String _messageBuffer = ""; + String _textInput = ""; // 0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ @override void initState() { @@ -23,9 +22,6 @@ class BluetoothDeviceSettingsState extends State { _bluetoothObject = widget.bluetoothObject; } - String _textInput = "0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - String _textOuput = ""; - ButtonStyle buttonStyle = ElevatedButton.styleFrom( foregroundColor: Colors.black, backgroundColor: const Color(0xFFFDE100), // Text Color (Foreground color) @@ -59,7 +55,7 @@ class BluetoothDeviceSettingsState extends State { ], ), Padding( - padding: const EdgeInsets.symmetric(horizontal: 16.0), + padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 0.0), child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ // const EdgeInsets defaultContentPadding = EdgeInsets.symmetric(horizontal: 16.0); Expanded( @@ -85,25 +81,23 @@ class BluetoothDeviceSettingsState extends State { )) ])), const Divider(), - ElevatedButton( - onPressed: () async { - _bluetoothObject.sendData(_textInput); - }, - style: buttonStyle, - child: const Text("Send Data"), - ), - ElevatedButton( - onPressed: () async { - setState(() { - _textOuput = ""; - }); - }, - style: buttonStyle, - child: const Text("Clear Output"), - ), - Text(_textOuput), Padding( - padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0), + padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 0.0), + child: + SizedBox( + width: double.infinity, + child: + ElevatedButton( + onPressed: () async { + _bluetoothObject.sendData(_textInput); + }, + style: buttonStyle, + child: const Text("Send Data"), + ), + ) + ), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), child: TextField( decoration: const InputDecoration( border: OutlineInputBorder(), diff --git a/lib/screens/bluetooth_screen.dart b/lib/screens/bluetooth_screen.dart index 449556f..469f5dc 100644 --- a/lib/screens/bluetooth_screen.dart +++ b/lib/screens/bluetooth_screen.dart @@ -50,7 +50,7 @@ class _BluetoothScreen extends State { _activeObject = null; _streamSubscription = null; if (widget.start) { - _startDiscovery(); + _initAsync(); } } @@ -63,8 +63,13 @@ class _BluetoothScreen extends State { debugPrint("called dispose"); } + Future _initAsync() async { + await _enablePermissions(); + await _enableBluetooth(); + await _startDiscovery(); + } + Future _enablePermissions() async { - debugPrint("Test"); PermissionStatus bluetoothScan = await Permission.bluetoothScan.request(); PermissionStatus bluetoothConnect = await Permission.bluetoothConnect.request(); bool granted = bluetoothScan.isGranted && bluetoothConnect.isGranted; @@ -74,7 +79,14 @@ class _BluetoothScreen extends State { } Future _enableBluetooth() async { - if (!_enabledPermissions) { + if (_enabledBluetooth) { + return; + } + BluetoothState state = await FlutterBluetoothSerial.instance.state; + if (state == BluetoothState.STATE_ON) { + setState(() { + _enabledBluetooth = true; + }); return; } bool? enabled = await FlutterBluetoothSerial.instance.requestEnable(); @@ -85,7 +97,8 @@ class _BluetoothScreen extends State { } Future _startDiscovery() async { - if (!_enabledPermissions) { + debugPrint("enabled: $_enabledPermissions"); + if (!_enabledPermissions && !_enabledBluetooth) { return; } setState(() => _isDiscovering = true); @@ -137,9 +150,6 @@ class _BluetoothScreen extends State { _activeObject!.isConnected ? await bluetoothObject.disconnectDevice() : await bluetoothObject.connectDevice(); } - //stty -F /dev/service 19200 parenb -parodd -cstopb cs8 - //cat /dev/service | xargs -n 1 /home/script/automaticcloud.sh - @override Widget build(BuildContext context) { return Scaffold(