Bluetooth.dart
4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import 'package:flutter/material.dart';
import 'package:Smart_Medicine_Box/src/screens/SettingPage.dart';
import 'package:flutter_blue/flutter_blue.dart';
class Bluetooth extends StatefulWidget {
@override
_BluetoothState createState() => _BluetoothState();
}
FlutterBlue flutterBlue;
class _BluetoothState extends State<Bluetooth> {
@override
void initState() {
super.initState();
flutterBlue = FlutterBlue.instance;
}
void _startscan() {
flutterBlue.startScan(timeout: Duration(seconds: 12));
var subscription = flutterBlue.scanResults.listen((results) {
// do something with scan results
for (ScanResult r in results) {
print(
'Device Name : ${r.device.name} // Device ID : ${r.device.id} // Device rssi: ${r.rssi}');
}
});
}
Widget build(BuildContext context) {
Widget _buildItem(ScanResult s) {
return ListTile(
leading: Text(s.rssi.toString()),
title: Text(s.device.name),
subtitle: Text(s.device.id.id),
onTap: () =>
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return Device(device: s.device);
})),
);
}
Widget _buildList(List<ScanResult> scanResults) {
return Column(
children: scanResults.map((v) => _buildItem(v)).toList(),
);
}
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.black),
backgroundColor: Colors.white,
title: Text(
'Smart Medicine Box',
style: TextStyle(
color: Colors.black,
fontSize: 20,
fontFamily: 'Noto',
fontWeight: FontWeight.bold),
),
actions: [
IconButton(
icon: Icon(
Icons.settings,
color: Colors.black,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => SettingPage(),
));
},
)
],
),
drawer: Drawer(
child: ListView(
children: [
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Test 1'),
onTap: () {},
),
ListTile(
title: Text('Test 2'),
onTap: () {},
),
ListTile(
title: Text('Test 3'),
onTap: () {},
),
],
),
),
body: SingleChildScrollView(
child: StreamBuilder<List<ScanResult>>(
stream: FlutterBlue.instance.scanResults,
initialData: [],
builder: (c, snapshot) {
return _buildList(snapshot.data);
}),
),
floatingActionButton: StreamBuilder<bool>(
stream: FlutterBlue.instance.isScanning,
initialData: false,
builder: (c, snapshot) {
if (snapshot.data) {
return FloatingActionButton(
child: Icon(Icons.stop),
onPressed: () => FlutterBlue.instance.stopScan(),
backgroundColor: Colors.red,
);
} else {
return FloatingActionButton(
child: Icon(Icons.search),
onPressed: () => FlutterBlue.instance
.startScan(timeout: Duration(seconds: 4)));
}
},
),
),
);
}
}
class Device extends StatefulWidget {
Device({Key key, this.device}) : super(key: key);
final BluetoothDevice device;
@override
_DeviceState createState() => _DeviceState();
}
class _DeviceState extends State<Device> {
void initState() {
super.initState();
widget.device.connect();
}
@override
void dispose() {
widget.device.disconnect();
super.dispose();
}
Widget _buildIconButton() {
return StreamBuilder<BluetoothDeviceState>(
stream: widget.device.state,
initialData: BluetoothDeviceState.connecting,
builder: (c, snapshot) {
if (snapshot.data != BluetoothDeviceState.connected)
return Icon(Icons.warning);
return IconButton(
icon: Icon(Icons.bluetooth_searching),
onPressed: () => widget.device.discoverServices(),
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.device.name),
actions: <Widget>[
_buildIconButton(),
],
),
body: Text('hello'),
);
}
}