-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
172 lines (164 loc) · 5.08 KB
/
main.dart
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
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:bluetooth_connector/bluetooth_connector.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
BluetoothConnector flutterbluetoothadapter = BluetoothConnector();
StreamSubscription? _btConnectionStatusListener, _btReceivedMessageListener;
String _connectionStatus = "NONE";
List<BtDevice> devices = [];
String? _recievedMessage;
TextEditingController _controller = TextEditingController();
@override
void initState() {
super.initState();
flutterbluetoothadapter
.initBlutoothConnection("20585adb-d260-445e-934b-032a2c8b2e14");
flutterbluetoothadapter
.checkBluetooth()
.then((value) => print(value.toString()));
_startListening();
}
_startListening() {
_btConnectionStatusListener =
flutterbluetoothadapter.connectionStatus().listen((dynamic status) {
setState(() {
_connectionStatus = status.toString();
});
});
_btReceivedMessageListener =
flutterbluetoothadapter.receiveMessages().listen((dynamic newMessage) {
setState(() {
_recievedMessage = newMessage.toString();
});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
fit: FlexFit.tight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: OutlinedButton(
onPressed: () async {
await flutterbluetoothadapter.startServer();
},
child: const Text('LISTEN'),
),
),
),
Flexible(
fit: FlexFit.tight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: OutlinedButton(
onPressed: () async {
devices = await flutterbluetoothadapter.getDevices();
setState(() {});
},
child: const Text('LIST DEVICES'),
),
),
)
],
),
Text("STATUS - $_connectionStatus"),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8.0,
vertical: 20,
),
child: ListView(
shrinkWrap: true,
children: _createDevices(),
),
),
Text(
_recievedMessage ?? "NO MESSAGE",
style: const TextStyle(fontSize: 24),
),
Row(
children: <Widget>[
Flexible(
flex: 4,
fit: FlexFit.tight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: _controller,
decoration:
const InputDecoration(hintText: "Write message"),
),
),
),
Flexible(
fit: FlexFit.tight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: OutlinedButton(
onPressed: () {
flutterbluetoothadapter.sendMessage(
_controller.text ?? "no msg",
sendByteByByte: false);
// flutterbluetoothadapter.sendMessage(".",
// sendByteByByte: true);
_controller.text = "";
},
child: const Text('SEND'),
),
),
)
],
)
],
),
),
);
}
_createDevices() {
if (devices.isEmpty) {
return [
const Center(
child: Text("No Paired Devices listed..."),
)
];
}
List<Widget> deviceList = [];
devices.forEach((element) {
deviceList.add(
InkWell(
key: UniqueKey(),
onTap: () {
flutterbluetoothadapter.startClient(devices.indexOf(element), true);
},
child: Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(border: Border.all()),
child: Text(
element.name.toString(),
style: const TextStyle(fontSize: 18),
),
),
),
);
});
return deviceList;
}
}