@@ -51,7 +51,8 @@ class BluetoothDevice {
51
51
..characteristicUuid = characteristic.uuid.toString ()
52
52
..serviceUuid = characteristic.serviceUuid.toString ();
53
53
54
- print ('remoteId: ${id .toString ()} characteristicUuid: ${characteristic .uuid .toString ()} serviceUuid: ${characteristic .serviceUuid .toString ()}' );
54
+ print (
55
+ 'remoteId: ${id .toString ()} characteristicUuid: ${characteristic .uuid .toString ()} serviceUuid: ${characteristic .serviceUuid .toString ()}' );
55
56
56
57
await FlutterBlue .instance._channel
57
58
.invokeMethod ('readCharacteristic' , request.writeToBuffer ());
@@ -82,8 +83,7 @@ class BluetoothDevice {
82
83
83
84
return await FlutterBlue .instance._descriptorReadChannel
84
85
.receiveBroadcastStream ()
85
- .map ((buffer) =>
86
- new protos.ReadDescriptorResponse .fromBuffer (buffer))
86
+ .map ((buffer) => new protos.ReadDescriptorResponse .fromBuffer (buffer))
87
87
.where ((p) =>
88
88
(p.request.remoteId == request.remoteId) &&
89
89
(p.request.descriptorUuid == request.descriptorUuid) &&
@@ -99,40 +99,46 @@ class BluetoothDevice {
99
99
/// guaranteed and will return immediately with success.
100
100
/// [CharacteristicWriteType.withResponse] : the method will return after the
101
101
/// write operation has either passed or failed.
102
- Future <Null > writeCharacteristic (BluetoothCharacteristic characteristic, List <int > value,
102
+ Future <Null > writeCharacteristic (
103
+ BluetoothCharacteristic characteristic, List <int > value,
103
104
{CharacteristicWriteType type =
104
105
CharacteristicWriteType .withoutResponse}) async {
105
106
var request = protos.WriteCharacteristicRequest .create ()
106
107
..remoteId = id.toString ()
107
108
..characteristicUuid = characteristic.uuid.toString ()
108
109
..serviceUuid = characteristic.serviceUuid.toString ()
109
- ..writeType = protos.WriteCharacteristicRequest_WriteType .valueOf (type.index)
110
+ ..writeType =
111
+ protos.WriteCharacteristicRequest_WriteType .valueOf (type.index)
110
112
..value = value;
111
113
112
114
var result = await FlutterBlue .instance._channel
113
115
.invokeMethod ('writeCharacteristic' , request.writeToBuffer ());
114
116
115
- if (type == CharacteristicWriteType .withoutResponse) {
117
+ if (type == CharacteristicWriteType .withoutResponse) {
116
118
return result;
117
119
}
118
120
119
121
return await FlutterBlue .instance._methodStream
120
- .where ((m) => m.method == "WriteCharacteristicResponse" )
121
- .map ((m) => m.arguments)
122
- .map ((buffer) => new protos.WriteCharacteristicResponse .fromBuffer (buffer))
123
- .where ((p) =>
124
- (p.request.remoteId == request.remoteId) &&
125
- (p.request.characteristicUuid == request.characteristicUuid) &&
126
- (p.request.serviceUuid == request.serviceUuid))
127
- .first
128
- .then ((w) => w.success)
129
- .then ((success) => (! success) ? throw new Exception ('Failed to write the characteristic' ) : null )
130
- .then ((_) => characteristic.value = value)
131
- .then ((_) => null );
122
+ .where ((m) => m.method == "WriteCharacteristicResponse" )
123
+ .map ((m) => m.arguments)
124
+ .map ((buffer) =>
125
+ new protos.WriteCharacteristicResponse .fromBuffer (buffer))
126
+ .where ((p) =>
127
+ (p.request.remoteId == request.remoteId) &&
128
+ (p.request.characteristicUuid == request.characteristicUuid) &&
129
+ (p.request.serviceUuid == request.serviceUuid))
130
+ .first
131
+ .then ((w) => w.success)
132
+ .then ((success) => (! success)
133
+ ? throw new Exception ('Failed to write the characteristic' )
134
+ : null )
135
+ .then ((_) => characteristic.value = value)
136
+ .then ((_) => null );
132
137
}
133
138
134
139
/// Writes the value of a descriptor
135
- Future <Null > writeDescriptor (BluetoothDescriptor descriptor, List <int > value) async {
140
+ Future <Null > writeDescriptor (
141
+ BluetoothDescriptor descriptor, List <int > value) async {
136
142
var request = protos.WriteDescriptorRequest .create ()
137
143
..remoteId = id.toString ()
138
144
..descriptorUuid = descriptor.uuid.toString ()
@@ -148,13 +154,15 @@ class BluetoothDevice {
148
154
.map ((m) => m.arguments)
149
155
.map ((buffer) => new protos.WriteDescriptorResponse .fromBuffer (buffer))
150
156
.where ((p) =>
151
- (p.request.remoteId == request.remoteId) &&
152
- (p.request.descriptorUuid == request.descriptorUuid) &&
153
- (p.request.characteristicUuid == request.characteristicUuid) &&
154
- (p.request.serviceUuid == request.serviceUuid))
157
+ (p.request.remoteId == request.remoteId) &&
158
+ (p.request.descriptorUuid == request.descriptorUuid) &&
159
+ (p.request.characteristicUuid == request.characteristicUuid) &&
160
+ (p.request.serviceUuid == request.serviceUuid))
155
161
.first
156
162
.then ((w) => w.success)
157
- .then ((success) => (! success) ? throw new Exception ('Failed to write the descriptor' ) : null )
163
+ .then ((success) => (! success)
164
+ ? throw new Exception ('Failed to write the descriptor' )
165
+ : null )
158
166
.then ((_) => descriptor.value = value)
159
167
.then ((_) => null );
160
168
}
@@ -168,24 +176,24 @@ class BluetoothDevice {
168
176
..characteristicUuid = characteristic.uuid.toString ()
169
177
..enable = notify;
170
178
171
- await FlutterBlue .instance._channel.invokeMethod ('setNotification' , request.writeToBuffer ());
179
+ await FlutterBlue .instance._channel
180
+ .invokeMethod ('setNotification' , request.writeToBuffer ());
172
181
173
182
return await FlutterBlue .instance._methodStream
174
183
.where ((m) => m.method == "SetNotificationResponse" )
175
184
.map ((m) => m.arguments)
176
- .map ((buffer) =>
177
- new protos.SetNotificationResponse .fromBuffer (buffer))
185
+ .map ((buffer) => new protos.SetNotificationResponse .fromBuffer (buffer))
178
186
.where ((p) =>
179
- (p.remoteId == request.remoteId) &&
180
- (p.characteristic.uuid == request.characteristicUuid) &&
181
- (p.characteristic.serviceUuid == request.serviceUuid))
187
+ (p.remoteId == request.remoteId) &&
188
+ (p.characteristic.uuid == request.characteristicUuid) &&
189
+ (p.characteristic.serviceUuid == request.serviceUuid))
182
190
.first
183
191
.then ((p) => new BluetoothCharacteristic .fromProto (p.characteristic))
184
192
.then ((c) {
185
- characteristic.updateDescriptors (c.descriptors);
186
- characteristic.value = c.value;
187
- return (c.isNotifying == notify);
188
- });
193
+ characteristic.updateDescriptors (c.descriptors);
194
+ characteristic.value = c.value;
195
+ return (c.isNotifying == notify);
196
+ });
189
197
}
190
198
191
199
/// Notifies when the characteristic's value has changed.
@@ -199,17 +207,17 @@ class BluetoothDevice {
199
207
.map ((p) => new BluetoothCharacteristic .fromProto (p.characteristic))
200
208
.where ((c) => c.uuid == characteristic.uuid)
201
209
.map ((c) {
202
- characteristic.updateDescriptors (c.descriptors);
203
- characteristic.value = c.value;
204
- return c.value;
205
- });
210
+ characteristic.updateDescriptors (c.descriptors);
211
+ characteristic.value = c.value;
212
+ return c.value;
213
+ });
206
214
}
207
215
208
216
/// The current connection state of the device
209
- Future <BluetoothDeviceState > get state =>
210
- FlutterBlue .instance._channel .invokeMethod ('deviceState' , id.toString ())
211
- .then ((buffer) => new protos.DeviceStateResponse .fromBuffer (buffer))
212
- .then ((p) => BluetoothDeviceState .values[p.state.value]);
217
+ Future <BluetoothDeviceState > get state => FlutterBlue .instance._channel
218
+ .invokeMethod ('deviceState' , id.toString ())
219
+ .then ((buffer) => new protos.DeviceStateResponse .fromBuffer (buffer))
220
+ .then ((p) => BluetoothDeviceState .values[p.state.value]);
213
221
214
222
/// Notifies when the device connection state has changed
215
223
Stream <BluetoothDeviceState > onStateChanged () {
0 commit comments