-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
bluetooth_descriptor.dart
89 lines (76 loc) · 3.22 KB
/
bluetooth_descriptor.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
// Copyright 2017, Paul DeMarco.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of flutter_blue;
class BluetoothDescriptor {
static final Guid cccd = new Guid("00002902-0000-1000-8000-00805f9b34fb");
final Guid uuid;
final DeviceIdentifier deviceId;
final Guid serviceUuid;
final Guid characteristicUuid;
BehaviorSubject<List<int>> _value;
Stream<List<int>> get value => _value.stream;
List<int> get lastValue => _value.value ?? [];
BluetoothDescriptor.fromProto(protos.BluetoothDescriptor p)
: uuid = new Guid(p.uuid),
deviceId = new DeviceIdentifier(p.remoteId),
serviceUuid = new Guid(p.serviceUuid),
characteristicUuid = new Guid(p.characteristicUuid),
_value = BehaviorSubject.seeded(p.value);
/// Retrieves the value of a specified descriptor
Future<List<int>> read() async {
var request = protos.ReadDescriptorRequest.create()
..remoteId = deviceId.toString()
..descriptorUuid = uuid.toString()
..characteristicUuid = characteristicUuid.toString()
..serviceUuid = serviceUuid.toString();
await FlutterBlue.instance._channel
.invokeMethod('readDescriptor', request.writeToBuffer());
return FlutterBlue.instance._methodStream
.where((m) => m.method == "ReadDescriptorResponse")
.map((m) => m.arguments)
.map((buffer) => new protos.ReadDescriptorResponse.fromBuffer(buffer))
.where((p) =>
(p.request.remoteId == request.remoteId) &&
(p.request.descriptorUuid == request.descriptorUuid) &&
(p.request.characteristicUuid == request.characteristicUuid) &&
(p.request.serviceUuid == request.serviceUuid))
.map((d) => d.value)
.first
.then((d) {
_value.add(d);
return d;
});
}
/// Writes the value of a descriptor
Future<Null> write(List<int> value) async {
var request = protos.WriteDescriptorRequest.create()
..remoteId = deviceId.toString()
..descriptorUuid = uuid.toString()
..characteristicUuid = characteristicUuid.toString()
..serviceUuid = serviceUuid.toString()
..value = value;
await FlutterBlue.instance._channel
.invokeMethod('writeDescriptor', request.writeToBuffer());
return FlutterBlue.instance._methodStream
.where((m) => m.method == "WriteDescriptorResponse")
.map((m) => m.arguments)
.map((buffer) => new protos.WriteDescriptorResponse.fromBuffer(buffer))
.where((p) =>
(p.request.remoteId == request.remoteId) &&
(p.request.descriptorUuid == request.descriptorUuid) &&
(p.request.characteristicUuid == request.characteristicUuid) &&
(p.request.serviceUuid == request.serviceUuid))
.first
.then((w) => w.success)
.then((success) => (!success)
? throw new Exception('Failed to write the descriptor')
: null)
.then((_) => _value.add(value))
.then((_) => null);
}
@override
String toString() {
return 'BluetoothDescriptor{uuid: $uuid, deviceId: $deviceId, serviceUuid: $serviceUuid, characteristicUuid: $characteristicUuid, value: ${_value.value}}';
}
}