Skip to content

Commit

Permalink
working search and print maps. also new font
Browse files Browse the repository at this point in the history
  • Loading branch information
SuddenDev committed Jan 30, 2020
1 parent 4312891 commit 4508144
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"flutter_blue","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_macos","shared_preferences_web"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]}]}
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"flutter_blue","dependencies":[]},{"name":"path_provider","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_macos","shared_preferences_web"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]}]}
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="ALINE"
android:label="aline"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
Expand Down
Binary file added fonts/Poppins-Bold.ttf
Binary file not shown.
Binary file added fonts/Poppins-Light.ttf
Binary file not shown.
Binary file added fonts/Poppins-Regular.ttf
Binary file not shown.
Binary file added fonts/Poppins-SemiBold.ttf
Binary file not shown.
15 changes: 7 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:flutter/material.dart';
// import 'package:dynamic_theme/dynamic_theme.dart';
//import 'package:flutter_blue/flutter_blue.dart';
import 'package:flutter_blue/flutter_blue.dart';

// import 'package:open_light_app/screens/wifi_setter.dart';
// import 'package:open_light_app/screens/bluetooth_off.dart';
import 'package:open_light_app/screens/home/home.dart';
import 'package:open_light_app/screens/wifi_setter.dart';
import 'package:open_light_app/screens/bluetooth_off.dart';
// import 'package:open_light_app/screens/home/home.dart';

import 'package:open_light_app/utils/themes.dart';

Expand All @@ -16,8 +15,8 @@ class OpenLightApp extends StatelessWidget {
return MaterialApp(
title: 'Wifi Setter Via BLE',
debugShowCheckedModeBanner: false,
home: Home(),
/*StreamBuilder<BluetoothState>(
home: //Home(),
StreamBuilder<BluetoothState>(
stream: FlutterBlue.instance.state,
initialData: BluetoothState.unknown,
builder: (c, snapshot) {
Expand All @@ -26,7 +25,7 @@ class OpenLightApp extends StatelessWidget {
return WifiSetter();
}
return BluetoothOffScreen(state: state);
}),*/
}),
theme: lightTheme,
// darkTheme: darkTheme
);
Expand Down
176 changes: 126 additions & 50 deletions lib/screens/wifi_setter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:convert' show utf8, json;

import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:loading/indicator/line_scale_pulse_out_indicator.dart';
import 'package:loading/loading.dart';



Expand All @@ -15,90 +17,90 @@ class _WifiSetterState extends State<WifiSetter> {

final String serviceUUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
final String characteristicUUID = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
final String targetDeviceName = "ALINE";
final String targetDeviceName = "aline";

FlutterBlue flutterBlue = FlutterBlue.instance;
StreamSubscription<ScanResult> scanSubscription;

BluetoothDevice targetDevice;
BluetoothCharacteristic targetCharacteristic;
BluetoothDeviceState targetDeviceState;

List connectionTexts = [
"Searching for your light",
"Did you plug your aline in?",
"Still looking..."
];
String connectionText = "";
String connectionStatus = "";
bool gotJson = false;

String wifiString;
Map<String, dynamic> wifiMap;
Stream<List<int>> streamFromBle;


@override
void initState() {
super.initState();
startScan();
}

// start scan for BLE
void startScan() {
setState(() {
connectionText = "Start Scanning";


void _deviceStateSubscription() {
targetDevice.state.listen((s) {
setState(() {
targetDeviceState = s;
});
});
}

/// Start scan for BLE
void startScan() {

// TODO: ADD TIMER FOR THE TEXT SWAP

scanSubscription = flutterBlue.scan().listen((scanResult) {
print('scanning..,');
print(scanResult.device.name);
// print('scanning...');

if (scanResult.device.name.contains(targetDeviceName)) {
stopScan();

setState(() {
connectionText = "Found Target Device";
});

targetDevice = scanResult.device;

// Check device state
_deviceStateSubscription();
connectToDevice();
}
}, onDone: () => stopScan());
}

// stop scanning
/// Stop scanning
void stopScan() {
scanSubscription?.cancel();
scanSubscription = null;
}

// connect to device
/// Connect to device
void connectToDevice() async {
if (targetDevice == null) {
return;
}

setState(() {
connectionText = "Device Connecting";
});

await targetDevice.connect();

setState(() {
connectionText = "Device Connected";
connectionStatus = "connected";
});

discoverServices();
}

// disconnect from device
/// Disconnect from device
void disconnectFromDeivce() {
if (targetDevice == null) {
return;
}

targetDevice.disconnect();

setState(() {
connectionText = "Device Disconnected";
});
}

// discover services and connect to pre-defined service if available
/// Discover services and connect to pre-defined service if available
void discoverServices() async {
if (targetDevice == null) {
return;
Expand All @@ -110,15 +112,12 @@ class _WifiSetterState extends State<WifiSetter> {
service.characteristics.forEach((characteristics) {
if (characteristics.uuid.toString() == characteristicUUID) {
targetCharacteristic = characteristics;
setState(() {
connectionText = "All Ready with ${targetDevice.name}";
});


// Getting wifi networks
targetCharacteristic.setNotifyValue(true);
streamFromBle = targetCharacteristic.value;
streamFromBle.listen((data) {
if(data != null && data.isNotEmpty) {
//print(utf8.decode(data));
mapWiFiNetworks(data);
}
});
Expand All @@ -140,6 +139,7 @@ class _WifiSetterState extends State<WifiSetter> {
} else if(currentString == "@@end@@") {
setState(() {
wifiMap = json.decode(wifiString);
gotJson = true;
});
} else {
setState(() {
Expand Down Expand Up @@ -173,19 +173,95 @@ class _WifiSetterState extends State<WifiSetter> {

@override
Widget build(BuildContext context) {

print(targetDeviceState);

return Scaffold(
appBar: AppBar(
title: Text(connectionText),
),
appBar: PreferredSize(
preferredSize: Size.fromHeight(120.0),
child: Container(
child: Padding(
padding: EdgeInsets.only(top: 56.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Image.asset(
'assets/logo_a_light.png',
fit: BoxFit.contain,
height: 40,
),
],
)),
),
),
body: Container(
child: targetCharacteristic == null
? Center(
child: Text(
connectionText,
style: TextStyle(fontSize: 34, color: Colors.red),
),
)
: Column(
margin: EdgeInsets.only(
top: 100.0
),
child: targetDeviceState == BluetoothDeviceState.connected
? ListView.builder(
itemCount: gotJson ? wifiMap["networks"].length : 0 ,
itemBuilder: (BuildContext context, int index) {

if(!gotJson) {
return SearchingForBluetooth();
}

print(wifiMap["networks"][index]["ssid"]);

return GestureDetector(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(wifiMap["networks"][index]["ssid"]),
Text(wifiMap["networks"][index]["rssi"].toString()),
Text(wifiMap["networks"][index]["encryption"].toString()),
],
),
);

},
)
: SearchingForBluetooth()
),
);

}
}

class SearchingForBluetooth extends StatelessWidget {
const SearchingForBluetooth({
Key key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
height: 200.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Loading(
indicator: LineScalePulseOutIndicator(),
size: 80.0,
color: Theme.of(context).primaryColorLight,
),
),
SizedBox(height: 20.0 ),
Text(
"Searching for your light",
style: Theme.of(context).textTheme.title,
)
],
),
);
}
}


/*
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16),
Expand All @@ -210,7 +286,7 @@ class _WifiSetterState extends State<WifiSetter> {
),
)
],
)),
);
}
}
)
*/

12 changes: 10 additions & 2 deletions lib/utils/themes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ final Color _accentColor = new Color(0xffFFBE61);
/// Light Theme
final ThemeData lightTheme = ThemeData(
primaryColor: _baseColorLight,
primaryColorLight: _shadowColorLightBright,
primaryColorDark: _shadowColorLightDark,
accentColor: _accentColor,
scaffoldBackgroundColor: _baseColorLight,
dialogBackgroundColor: _baseColorLight
dialogBackgroundColor: _baseColorLight,
fontFamily: 'Poppins',
textTheme: TextTheme(
title: TextStyle(color: _shadowColorLightDark),
)
);

/// Dark Theme
final ThemeData darkTheme = ThemeData.dark().copyWith(
primaryColor: _baseColorDark,
primaryColorLight: _shadowColorDarkBright,
primaryColorDark: _shadowColorDarkDark,
accentColor: _accentColor,
scaffoldBackgroundColor: _baseColorDark,
dialogBackgroundColor: _baseColorDark
dialogBackgroundColor: _baseColorDark,
);


Expand Down
Loading

0 comments on commit 4508144

Please sign in to comment.