-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into home
- Loading branch information
Showing
24 changed files
with
713 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:bloc/bloc.dart'; | ||
import 'package:flutter_blue/flutter_blue.dart'; | ||
import 'package:treehousesble/app/app.dart'; | ||
import 'package:treehousesble/common/bloc/bluetooth_state.dart'; | ||
import 'package:treehousesble/common/constants/app_constants.dart'; | ||
import 'package:treehousesble/common/constants/constant.dart'; | ||
import 'package:treehousesble/common/constants/strings.dart'; | ||
import 'package:treehousesble/common/shared_pref/shared_pref.dart'; | ||
|
||
class BluetoothCubit extends Cubit<DataState> { | ||
BluetoothCubit() : super(StateDeviceNotConnected()); | ||
final FlutterBlue flutterBlue = FlutterBlue.instance; | ||
final List<BluetoothDevice> devicesList = []; | ||
final Map<Guid, List<int>> readValues = new Map<Guid, List<int>>(); | ||
BluetoothCharacteristic? characteristic; | ||
|
||
appStart() async { | ||
final bool firstTimeAppOpen = await SharedPref.getFirstTimeAppOpen(); | ||
if (firstTimeAppOpen) { | ||
emit(FirstTimeAppOpen()); | ||
} else { | ||
emit(NotFirstTimeAppOpen()); | ||
} | ||
} | ||
List<int> _sendCommand(String command) { | ||
return utf8.encode(command); | ||
} | ||
|
||
writeMessage(String command) async{ | ||
if(characteristic != null){ | ||
await characteristic?.write(_sendCommand(command), withoutResponse: true); | ||
print("SEND MESSAGE " + command); | ||
readMessage(); | ||
} | ||
} | ||
|
||
readMessage() async{ | ||
if(characteristic != null){ | ||
emit(StateReading()); | ||
List<int> response = await characteristic!.read(); | ||
var responseString = utf8.decode(response); | ||
emit(StateReadSuccess(data: responseString)); | ||
} | ||
|
||
} | ||
|
||
fetchDeviceList(bool filterPi) { | ||
emit(StateLoading()); | ||
print("FETCH DEVICE LIST"); | ||
flutterBlue.connectedDevices | ||
.asStream() | ||
.listen((List<BluetoothDevice> devices) { | ||
for (BluetoothDevice device in devices) { | ||
_addDeviceTolist(device, filterPi); | ||
} | ||
}); | ||
flutterBlue.scanResults.listen((List<ScanResult> results) { | ||
print("Scan result " + results.length.toString()); | ||
for (ScanResult result in results) { | ||
_addDeviceTolist(result.device, filterPi); | ||
} | ||
}); | ||
flutterBlue.startScan(); | ||
} | ||
|
||
void _addDeviceTolist(BluetoothDevice device, bool filterPi) { | ||
print("Filter pi " + device.name); | ||
if (!devicesList.contains(device) && checkIfPi(device, filterPi)) { | ||
devicesList.add(device); | ||
} | ||
emit(StateIniital()); | ||
emit(StateFoundDevices(list: devicesList)); | ||
// if (devicesList.length > 0) ); | ||
} | ||
|
||
fetchServicesAndConnect(BluetoothDevice device) async { | ||
emit(StateLoading()); | ||
try { | ||
await device.connect(timeout: Duration(seconds: 20)); | ||
discoverServices(device); | ||
} catch (e) { | ||
if(e.toString().contains("already")){ | ||
discoverServices(device); | ||
}else{ | ||
emit(StateError(message: "Unable to connect please try again")); | ||
} | ||
} | ||
} | ||
|
||
checkDeviceConnected() async { | ||
if (characteristic == null) { | ||
emit(StateDeviceNotConnected()); | ||
var connected = await flutterBlue.connectedDevices; | ||
if (connected.length > 0) fetchServicesAndConnect(connected[0]); | ||
} else { | ||
emit(StateDeviceConnected(characteristic: characteristic!)); | ||
} | ||
} | ||
|
||
bool checkIfPi(BluetoothDevice device, bool filterPi) { | ||
if (filterPi) { | ||
bool isPi = false; | ||
for (int i = 0; i < AppConstants.PI_ADDRESS.length; i++) { | ||
if (device.id | ||
.toString() | ||
.toLowerCase() | ||
.startsWith(AppConstants.PI_ADDRESS[i].toLowerCase())) { | ||
isPi = true; | ||
break; | ||
} | ||
} | ||
return isPi; | ||
} | ||
return true; | ||
} | ||
|
||
void discoverServices(BluetoothDevice device)async{ | ||
List<BluetoothService> services = await device.discoverServices(); | ||
for (BluetoothService service in services) { | ||
if (service.uuid.toString() == Strings.BLUETOOTH_UUID) { | ||
List<BluetoothCharacteristic> characteristics = | ||
service.characteristics; | ||
if (characteristics.length > 0) { | ||
characteristic = characteristics[0]; | ||
emit(StateDeviceConnected(characteristic: characteristic!)); | ||
print("CONNECTED TO DEVICE"); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:flutter_blue/flutter_blue.dart'; | ||
|
||
class DataState extends Equatable { | ||
final dynamic data; | ||
|
||
const DataState({this.data}); | ||
|
||
@override | ||
List<Object?> get props => [...data]; | ||
} | ||
|
||
class FirstTimeAppOpen extends DataState { | ||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
|
||
class NotFirstTimeAppOpen extends DataState { | ||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
|
||
class StateIniital extends DataState {} | ||
|
||
class StateLoading extends DataState {} | ||
class StateReading extends DataState {} | ||
|
||
class StateError extends DataState { | ||
final String message; | ||
|
||
const StateError({required this.message}); | ||
|
||
@override | ||
List<Object?> get props => [message]; | ||
} | ||
|
||
class StateNoData extends DataState { | ||
final String message; | ||
|
||
const StateNoData({required this.message}); | ||
|
||
List<Object?> get props => [message]; | ||
} | ||
|
||
class StateReadSuccess extends DataState { | ||
final dynamic data; | ||
|
||
const StateReadSuccess({required this.data}) : super(data: data); | ||
|
||
@override | ||
List<Object?> get props => [...data]; | ||
} | ||
|
||
class StateFoundDevices extends DataState { | ||
final List<BluetoothDevice> list; | ||
|
||
const StateFoundDevices({required this.list}) : super(data: list); | ||
|
||
@override | ||
List<Object?> get props => [...data]; | ||
} | ||
|
||
|
||
class StateDeviceConnected extends DataState { | ||
final BluetoothCharacteristic characteristic; | ||
|
||
const StateDeviceConnected({required this.characteristic}); | ||
|
||
List<Object?> get props => []; | ||
} | ||
|
||
class StateDeviceNotConnected extends DataState { | ||
|
||
const StateDeviceNotConnected(); | ||
|
||
List<Object?> get props => []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class AppConstants{ | ||
static var PI_ADDRESS = [ | ||
"B8:27:EB", | ||
"DC:A6:32", | ||
"E4:5F:01", | ||
"B8-27-EB", | ||
"DC-A6-32", | ||
"E4-5F-01", | ||
"B827.EB", | ||
"DCA6.32", | ||
"E45F.01", | ||
"b8:27:eb", | ||
"dc:a6:32", | ||
"e4:5f:01", | ||
"b8-27-eb", | ||
"dc-a6-32", | ||
"e4-5f-01", | ||
"b827.eb", | ||
"dca6.32", | ||
"e45f.01" | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export 'assets.dart'; | ||
|
||
export 'strings.dart'; | ||
export 'fonts.dart'; | ||
export 'app_constants.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Env { | ||
Env(this.uuid); | ||
final String uuid; | ||
} | ||
|
||
mixin EnvValue { | ||
static final Env development = | ||
Env('6e400001-b5a3-f393-e0a9-e50e24dcca9e'); | ||
static final Env staging = | ||
Env('6e400001-b5a3-f393-e0a9-e50e24dcca9e'); | ||
static final Env production = | ||
Env('6e400001-b5a3-f393-e0a9-e50e24dcca9e'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
class Strings { | ||
static const BLUETOOTH_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"; | ||
static const APP_TITLE = "Treehouses Remote II"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:treehousesble/common/constants/env.dart'; | ||
|
||
import 'multi_bloc_listing.dart'; | ||
|
||
class MultiRepoListing extends StatelessWidget { | ||
final Widget child; | ||
const MultiRepoListing({required this.child}); | ||
final Env env; | ||
|
||
const MultiRepoListing({required this.child, required this.env}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MultiRepositoryProvider(providers: [], child: MultiBlocListing(child: child)); | ||
return MultiRepositoryProvider(providers: [ | ||
RepositoryProvider<Env>(create: (context) => env, lazy: true), | ||
], child: MultiBlocListing(child: child)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.