Commit 0c4e2558 authored by Nicolas Richard Walter Boeckh's avatar Nicolas Richard Walter Boeckh 💬

Small async / context err

parent 24090d57
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_blue","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\flutter_blue-0.6.3+1\\\\","dependencies":[]},{"name":"geolocator","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\geolocator-5.2.1\\\\","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\google_api_availability-2.0.2\\\\","dependencies":[]},{"name":"location_permissions","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\location_permissions-2.0.4+1\\\\","dependencies":[]},{"name":"path_provider","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider-1.6.0\\\\","dependencies":[]},{"name":"permission_handler","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\permission_handler-4.2.0+hotfix.3\\\\","dependencies":[]},{"name":"shared_preferences","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences-0.4.3\\\\","dependencies":[]},{"name":"sqflite","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-1.2.0\\\\","dependencies":[]}],"android":[{"name":"flutter_blue","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\flutter_blue-0.6.3+1\\\\","dependencies":[]},{"name":"geolocator","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\geolocator-5.2.1\\\\","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\google_api_availability-2.0.2\\\\","dependencies":[]},{"name":"location_permissions","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\location_permissions-2.0.4+1\\\\","dependencies":[]},{"name":"path_provider","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider-1.6.0\\\\","dependencies":[]},{"name":"permission_handler","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\permission_handler-4.2.0+hotfix.3\\\\","dependencies":[]},{"name":"shared_preferences","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences-0.4.3\\\\","dependencies":[]},{"name":"sqflite","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-1.2.0\\\\","dependencies":[]}],"macos":[{"name":"sqflite","path":"C:\\\\src\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\sqflite-1.2.0\\\\","dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"flutter_blue","dependencies":[]},{"name":"geolocator","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","dependencies":[]},{"name":"location_permissions","dependencies":[]},{"name":"path_provider","dependencies":[]},{"name":"permission_handler","dependencies":[]},{"name":"shared_preferences","dependencies":[]},{"name":"sqflite","dependencies":[]}],"date_created":"2020-02-20 17:12:51.524675","version":"1.14.6"}
\ No newline at end of file
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"flutter_blue","dependencies":[]},{"name":"geolocator","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","dependencies":[]},{"name":"location_permissions","dependencies":[]},{"name":"path_provider","dependencies":[]},{"name":"permission_handler","dependencies":[]},{"name":"shared_preferences","dependencies":[]},{"name":"sqflite","dependencies":[]}]}
\ No newline at end of file
......@@ -12,32 +12,21 @@ class BTLEHandler {
static bool started = false;
static Duration duration = new Duration(seconds: 1);
Timer t;
FlutterBlue _flutterBlueInstance = FlutterBlue.instance;
FlutterBlue fblueInstance = FlutterBlue.instance;
static const MethodChannel _btAdapterChannel = const MethodChannel(Channels.BLUETOOTH_ADAPTER);
factory BTLEHandler() => _singleton;
BTLEHandler._internal() {
_flutterBlueInstance.setLogLevel(LogLevel.info);
fblueInstance.setLogLevel(LogLevel.info);
}
startScan() =>
_flutterBlueInstance.startScan(timeout: Duration(seconds: 5));
stopScan() =>
_flutterBlueInstance.stopScan();
Future<bool> isScanning() => _flutterBlueInstance.isScanning.first;
Future<List<BluetoothDevice>> get connectedDevices => _flutterBlueInstance.connectedDevices;
bool isDeviceConnected() => _device != null;
Stream<List<ScanResult>> get scanResults => _flutterBlueInstance.scanResults;
Future<void> connect() => this._device?.let((it) => it.connect());
Future<dynamic> disconnect() => this._device?.let((it) {
......@@ -62,10 +51,6 @@ class BTLEHandler {
BluetoothDevice _device;
Future<bool> isBluetoothAvailable() => _flutterBlueInstance.isAvailable;
Future<bool> isBluetoothActive() => _flutterBlueInstance.isOn;
setDevice(BluetoothDevice device) => device?.let((it) => this._device = it);
bool switchState() {
......@@ -87,8 +72,5 @@ class BTLEHandler {
WakeService().modifyWakeService(false).then((value) => print('Device will stay awake $value'));
t.cancel();
}
Stream<BluetoothState> get state => _flutterBlueInstance.state;
}
\ No newline at end of file
......@@ -24,7 +24,7 @@ class BluetoothOffScreen extends StatelessWidget {
'Bluetooth Adapter is ${state.toString().substring(15)}.', // TODO internationalize
style: Theme.of(context)
.primaryTextTheme
.subtitle1
.subtitle
.copyWith(color: Colors.white),
),
],
......
......@@ -23,7 +23,7 @@ class BluetoothUnavailableScreen extends StatelessWidget {
AppLocalization.of(context).bluetoothUnsupported,
style: Theme.of(context)
.primaryTextTheme
.subtitle1
.subtitle
.copyWith(color: Colors.white),
),
],
......
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:logair_application/animation/forward_animation.dart';
import 'package:logair_application/handlers/bluetooth_le_handler.dart';
import 'package:logair_application/ui/dialog/bluetooth_dialog.dart';
class BluetoothButton extends StatefulWidget {
final Function onTapOn;
final Function onTapOff;
BluetoothButton({Key key, @required this.onTapOn, @required this.onTapOff}) : super(key: key);
BluetoothButton({Key key}) : super(key: key);
@override
_BluetoothButtonState createState() => _BluetoothButtonState(onTapOn: this.onTapOn, onTapOff: this.onTapOff);
_BluetoothButtonState createState() => _BluetoothButtonState();
}
class _BluetoothButtonState extends State<BluetoothButton> {
bool _bluetoothConnected = false;
final Function onTapOn;
final Function onTapOff;
_BluetoothButtonState({Key key, @required this.onTapOn, @required this.onTapOff});
_BluetoothButtonState({Key key});
@override
Widget build(BuildContext context) {
......@@ -34,16 +31,19 @@ class _BluetoothButtonState extends State<BluetoothButton> {
borderRadius: BorderRadius.circular(1000.0),
onTap: () {
print("onTap");
bool returnValue;
if (!this._bluetoothConnected)
this.onTapOn().then((value) {
print('Tap on $value');
if (value)
setState(() => {
_bluetoothConnected = !_bluetoothConnected
});
});
Navigator.push(
context,
PopForwardRoute(
page: BluetoothDialog(),
),
).then((value) => returnValue = value);
else
else {
BTLEHandler().disconnect();
return true;
}
this.onTapOff().then((value) {
print('Tap off $value');
if (value)
......
......@@ -34,10 +34,7 @@ class DataWidget extends StatelessWidget {
},
child: Text('Toggle BTLE Service'),
),
BluetoothButton(
onTapOn: () => _onTapOn(context),
onTapOff: () => _onTapOff(context),
),
BluetoothButton(),
]
),
);
......@@ -45,16 +42,5 @@ class DataWidget extends StatelessWidget {
}
}
_onTapOn(BuildContext context) async {
Navigator.push(
context,
PopForwardRoute(
page: BluetoothDialog(),
),
);
}
_onTapOff(BuildContext context) async {
BTLEHandler().disconnect();
return true;
}
......@@ -21,24 +21,37 @@ class _BluetoothDialogState extends State<BluetoothDialog> {
@override
Widget build(BuildContext context) {
BTLEHandler().isBluetoothAvailable().then((value) => setState(() {
bluetoothAvailable = true;
}));
if (!bluetoothAvailable)
BTLEHandler().fblueInstance.isAvailable.then((value) => setState(() {
bluetoothAvailable = true;
print("btAvail ? $bluetoothAvailable");
}));
return SafeArea(
child: Scaffold(
body: Center(
child: Column(
children: <Widget>[
// Carousel
Expanded(
flex: 9,
child: _bluetoothPane(bluetoothAvailable)
child: (bluetoothAvailable) ?
StreamBuilder<BluetoothState>(
stream: BTLEHandler().fblueInstance.state,
builder: (context, snapshot) {
final state = snapshot.data;
if (state == BluetoothState.on) {
return BluetoothSelectionDialog();
} else {
return BluetoothOffScreen(state: state);
}
},
) :
BluetoothUnavailableScreen()
),
Expanded(
flex: 1,
child: RaisedButton(
onPressed: () => Navigator.of(context).pop(),
onPressed: () => Navigator.of(context).pop(false),
child: Text("Back"),
)
),
......@@ -48,19 +61,4 @@ class _BluetoothDialogState extends State<BluetoothDialog> {
),
);
}
}
Widget _bluetoothPane(bluetoothAvailable) =>
(!bluetoothAvailable) ?
StreamBuilder<BluetoothState>(
stream: BTLEHandler().state,
builder: (context, snapshot) {
final state = snapshot.data;
if (state == BluetoothState.on) {
return BluetoothSelectionDialog();
} else {
return BluetoothOffScreen(state: state);
}
},
) :
BluetoothUnavailableScreen();
\ No newline at end of file
}
\ No newline at end of file
......@@ -37,11 +37,10 @@ class _BluetoothSelectionDialogState extends State<BluetoothSelectionDialog> {
@override
Widget build(BuildContext context) {
BTLEHandler().startScan();
PreferencesHandler().getPreferences().then((preferences) =>
setState(() => this._frequentDevices = List.from(json.decode((preferences.getString('json') ?? '[ ]')).map((device) => Device(name: device["name"], address: device["address"]))))
);
if (_frequentDevices != null)
PreferencesHandler().getPreferences().then((preferences) =>
setState(() => this._frequentDevices = List.from(json.decode((preferences.getString('json') ?? '[ ]')).map((device) => Device(name: device["name"], address: device["address"]))))
);
return BaseWidget(
builder: (context, sizingInfo) => Container(
......@@ -57,8 +56,8 @@ class _BluetoothSelectionDialogState extends State<BluetoothSelectionDialog> {
padding: EdgeInsets.all(0),
icon: Icon(Icons.arrow_back),
onPressed: () async {
if (await BTLEHandler().isScanning())
BTLEHandler().stopScan();
if (await BTLEHandler().fblueInstance.isScanning.first)
BTLEHandler().fblueInstance.stopScan();
Navigator.of(context).pop(false);
},
),
......@@ -69,7 +68,7 @@ class _BluetoothSelectionDialogState extends State<BluetoothSelectionDialog> {
trailing: IconButton(
padding: EdgeInsets.all(0),
icon: Icon(Icons.refresh),
onPressed: () => BTLEHandler().startScan(),
onPressed: () => BTLEHandler().fblueInstance.startScan(timeout: Duration(seconds: 5)),
),
),
Divider(),
......@@ -77,7 +76,7 @@ class _BluetoothSelectionDialogState extends State<BluetoothSelectionDialog> {
AppLocalization.of(context).bluetoothFrequent
),
StreamBuilder<List<ScanResult>>(
stream: BTLEHandler().scanResults.transform(
stream: BTLEHandler().fblueInstance.scanResults.transform(
new StreamTransformer.fromHandlers(
handleData: (List<ScanResult> list, EventSink<List<ScanResult>> sink) =>
sink.add(list.where((result) => _frequentDevices != null ? _frequentDevices.map((Device d) => d.address).contains(result.device.id.toString()) : false).toList())
......@@ -108,7 +107,7 @@ class _BluetoothSelectionDialogState extends State<BluetoothSelectionDialog> {
AppLocalization.of(context).bluetoothAvailable
),
StreamBuilder<List<ScanResult>>(
stream: BTLEHandler().scanResults.transform(
stream: BTLEHandler().fblueInstance.scanResults.transform(
new StreamTransformer.fromHandlers(
handleData: (List<ScanResult> list, EventSink<List<ScanResult>> sink) =>
sink.add(list.where((result) => _frequentDevices != null ? !_frequentDevices.map((Device d) => d.address).contains(result.device.id.toString()) : true).toList())
......@@ -121,7 +120,7 @@ class _BluetoothSelectionDialogState extends State<BluetoothSelectionDialog> {
(result) => BTLEScanTile(
result: result,
onTap: () {
BTLEHandler().stopScan();
BTLEHandler().fblueInstance.stopScan();
BTLEHandler().setDevice(result.device);
BTLEHandler().connect();
......
......@@ -246,7 +246,7 @@ packages:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.1"
version: "0.16.0"
intl_translation:
dependency: "direct main"
description:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment