Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception handling #27

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (localPropertiesFile.exists()) {

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
throw GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
Expand All @@ -25,7 +25,7 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 29
compileSdkVersion 31

lintOptions {
disable 'InvalidPackage'
Expand Down
1 change: 0 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="esp_provisioning_example"
android:icon="@mipmap/ic_launcher">
<activity
Expand Down
77 changes: 49 additions & 28 deletions example/lib/ble_screen/ble_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,65 +1,86 @@
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:esp_provisioning_example/ble_service.dart';
import 'package:flutter_ble_lib/flutter_ble_lib.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:rxdart/rxdart.dart';
import 'ble.dart';

class BleBloc extends Bloc<BleEvent, BleState> {
var bleService = BleService.getInstance();
StreamSubscription<ScanResult> _scanSubscription;
List<Map<String, dynamic>> bleDevices = new List<Map<String, dynamic>>();
StreamSubscription<List<ScanResult>> _scanSubscription;
List<Map<String, dynamic>> bleDevices = [];

BleBloc(BleState initialState) : super(initialState);

@override
Stream<BleState> mapEventToState(
BleEvent event,
) async* {
if (event is BleEventStart) {
yield* _mapStartToState();
} else if (event is BleEventDeviceUpdated) {
yield BleStateLoaded(List.from(event.bleDevices));
} else if (event is BleEventSelect) {
bleService.select(event.selectedDevice['peripheral']);
} else if (event is BleEventStopScan) {
BleBloc() : super(BleStateLoading()) {
on<BleEventStart>((event, emit) async {
await for (final result in _mapStartToState()) {
emit(result);
}
});
on<BleEventDeviceUpdated>((event, emit) {
emit(BleStateLoaded(List.from(event.bleDevices)));
});
on<BleEventPermissionDenied>((event, emit) async {
emit(BleStatePermissionDenied());
});
on<BleEventStopScan>((event, emit) async {
await bleService.stopScanBle();
}
});
on<BleEventSelect>((event, emit) async {
bleService.select(event.selectedDevice['peripheral']);
});
}

// @override
// Stream<BleState> mapEventToState(
// BleEvent event,
// ) async* {
// if (event is BleEventStart) {
// yield* _mapStartToState();
// } else if (event is BleEventDeviceUpdated) {
// yield BleStateLoaded(List.from(event.bleDevices));
// } else if (event is BleEventSelect) {
// bleService.select(event.selectedDevice['peripheral']);
// } else if (event is BleEventStopScan) {
// await bleService.stopScanBle();
// }
// }

Stream<BleState> _mapStartToState() async* {
var permissionIsGranted = await bleService.requestBlePermissions();
if (!permissionIsGranted) {
add(BleEventPermissionDenied());
return;
}
var bleState = await bleService.start();
if (bleState == BluetoothState.UNAUTHORIZED) {
if (bleState == BluetoothState.unauthorized) {
add(BleEventPermissionDenied());
return;
}
_scanSubscription?.cancel();
_scanSubscription = bleService
.scanBle()
.debounce((_) => TimerStream(true, Duration(milliseconds: 100)))
.listen((ScanResult scanResult) {
var bleDevice = BleDevice(scanResult);
if (scanResult.advertisementData.localName != null) {
var idx = bleDevices.indexWhere((e) => e['id'] == bleDevice.id);
.listen((List<ScanResult> scanResult) {
for (int i = 0; i < scanResult.length; i++) {
var bleDevice = BleDevice(scanResult[i]);
if (scanResult[i].advertisementData.localName != null) {
var idx = bleDevices.indexWhere((e) => e['id'] == bleDevice.id);

if (idx < 0) {
bleDevices.add(bleDevice.toMap());
} else {
bleDevices[idx] = bleDevice.toMap();
if (idx < 0) {
bleDevices.add(bleDevice.toMap());
} else {
bleDevices[idx] = bleDevice.toMap();
}
add(BleEventDeviceUpdated(bleDevices));
}
add(BleEventDeviceUpdated(bleDevices));
}
});
}

@override
Future<void> close() {
super.close();
_scanSubscription?.cancel();
bleService.stopScanBle();
return bleService.stopScanBle();
}
}
12 changes: 7 additions & 5 deletions example/lib/ble_screen/ble_device.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import 'package:collection/collection.dart';
import 'package:flutter_ble_lib/flutter_ble_lib.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';

class BleDevice {
final Peripheral peripheral;
final BluetoothDevice peripheral;
final String name;
int rssi;

String get id => peripheral.identifier;
String get id => peripheral.id.id;

BleDevice(ScanResult scanResult)
: peripheral = scanResult.peripheral,
name = scanResult.peripheral.name ?? scanResult.advertisementData.localName ?? "Unknown",
: peripheral = scanResult.device,
name = scanResult.device.name ??
scanResult.advertisementData.localName ??
"Unknown",
rssi = scanResult.rssi;

@override
Expand Down
1 change: 1 addition & 0 deletions example/lib/ble_screen/ble_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ abstract class BleEvent extends Equatable {
class BleEventStart extends BleEvent {}

class BleEventPermissionDenied extends BleEvent {}

class BleEventStopScan extends BleEvent {}

class BleEventDeviceUpdated extends BleEvent {
Expand Down
5 changes: 2 additions & 3 deletions example/lib/ble_screen/ble_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class BleScreen extends StatefulWidget {
}

class _BleScreenState extends State<BleScreen> {

void _showBottomSheet(Map<String, dynamic> item, BuildContext _context) {
BlocProvider.of<BleBloc>(_context).add(BleEventStopScan());
BlocProvider.of<BleBloc>(_context).add(BleEventSelect(item));
Expand Down Expand Up @@ -50,7 +49,7 @@ class _BleScreenState extends State<BleScreen> {
title: const Text('Scanning BLE devices'),
),
body: BlocProvider(
create: (BuildContext context) => BleBloc(BleStateLoading())..add(BleEventStart()),
create: (BuildContext context) => BleBloc()..add(BleEventStart()),
child: BlocBuilder<BleBloc, BleState>(
builder: (BuildContext context, BleState state) {
if (state is BleStatePermissionDenied) {
Expand All @@ -66,7 +65,7 @@ class _BleScreenState extends State<BleScreen> {
}

return Center(
child: SpinKitRipple(color: Theme.of(context).textSelectionColor),
child: SpinKitRipple(color: Colors.purple),
);
},
),
Expand Down
Loading