Skip to content

Commit

Permalink
Merge pull request #43 from ChangJoo-Park/master
Browse files Browse the repository at this point in the history
Fix #42 Error when meet unknown devices.
  • Loading branch information
AgainPsychoX authored Jul 1, 2019
2 parents 051ab9d + 85901d2 commit 52aa263
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion example/lib/BluetoothDeviceListEntry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BluetoothDeviceListEntry extends ListTile {
onLongPress: onLongPress,
enabled: enabled,
leading: Icon(Icons.devices), // @TODO . !BluetoothClass! class aware icon
title: Text(device.name),
title: Text(device.name ?? "Unknown device"),
subtitle: Text(device.address.toString()),
trailing: Row(
mainAxisSize: MainAxisSize.min,
Expand Down
33 changes: 19 additions & 14 deletions example/lib/DiscoveryPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DiscoveryPage extends StatefulWidget {
final bool start;

const DiscoveryPage({this.start = true});

@override
_DiscoveryPage createState() => new _DiscoveryPage();
}
Expand All @@ -18,13 +18,13 @@ class _DiscoveryPage extends State<DiscoveryPage> {
StreamSubscription<BluetoothDiscoveryResult> _streamSubscription;
List<BluetoothDiscoveryResult> results = List<BluetoothDiscoveryResult>();
bool isDiscovering;

_DiscoveryPage();

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

isDiscovering = widget.start;
if (isDiscovering) {
_startDiscovery();
Expand All @@ -44,7 +44,7 @@ class _DiscoveryPage extends State<DiscoveryPage> {
_streamSubscription = FlutterBluetoothSerial.instance.startDiscovery().listen((r) {
setState(() { results.add(r); });
});

_streamSubscription.onDone(() {
setState(() { isDiscovering = false; });
});
Expand All @@ -59,22 +59,15 @@ class _DiscoveryPage extends State<DiscoveryPage> {

super.dispose();
}

@override
Widget build(BuildContext context) {
List<BluetoothDeviceListEntry> list = results.map((result) => BluetoothDeviceListEntry(
device: result.device,
rssi: result.rssi,
onTap: () {
Navigator.of(context).pop(result.device);
},
)).toList();
return Scaffold(
appBar: AppBar(
title: isDiscovering ? Text('Discovering devices') : Text('Discovered devices'),
actions: <Widget>[
(
isDiscovering ?
isDiscovering ?
FittedBox(child: Container(
margin: new EdgeInsets.all(16.0),
child: CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.white))
Expand All @@ -87,7 +80,19 @@ class _DiscoveryPage extends State<DiscoveryPage> {
)
],
),
body: ListView(children: list)
body: ListView.builder(
itemCount: results.length,
itemBuilder: (BuildContext context, index) {
BluetoothDiscoveryResult result = results[index];
return BluetoothDeviceListEntry(
device: result.device,
rssi: result.rssi,
onTap: () {
Navigator.of(context).pop(result.device);
},
);
},
)
);
}
}

0 comments on commit 52aa263

Please sign in to comment.