forked from pauldemarco/flutter_blue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth_characteristic.dart
90 lines (81 loc) · 2.9 KB
/
bluetooth_characteristic.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
// 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 BluetoothCharacteristic {
final Guid uuid;
final Guid serviceUuid; // The service that this characteristic belongs to.
final Guid
secondaryServiceUuid; // The nested service that this characteristic belongs to.
final CharacteristicProperties properties;
final List<BluetoothDescriptor> descriptors;
bool get isNotifying {
try {
var cccd = descriptors.singleWhere((d) =>
d.uuid == BluetoothDescriptor.CCCD);
return ((cccd.value[0] & 0x01) > 0 || (cccd.value[0] & 0x02) > 0);
} catch(e) {
return false;
}
}
List<int> value;
BluetoothCharacteristic(
{@required this.uuid,
@required this.serviceUuid,
this.secondaryServiceUuid,
@required this.descriptors,
@required this.properties});
BluetoothCharacteristic.fromProto(protos.BluetoothCharacteristic p)
: uuid = new Guid(p.uuid),
serviceUuid = new Guid(p.serviceUuid),
secondaryServiceUuid = (p.secondaryServiceUuid.length > 0) ? new Guid(p.secondaryServiceUuid): null,
descriptors = p.descriptors
.map((d) => new BluetoothDescriptor.fromProto(d))
.toList(),
properties = new CharacteristicProperties.fromProto(p.properties),
value = p.value;
void updateDescriptors(List<BluetoothDescriptor> newDescriptors) {
for(var d in descriptors) {
for(var newD in newDescriptors){
if(d.uuid == newD.uuid) {
d.value = newD.value;
}
}
}
}
}
enum CharacteristicWriteType { withResponse, withoutResponse }
class CharacteristicProperties {
final bool broadcast;
final bool read;
final bool writeWithoutResponse;
final bool write;
final bool notify;
final bool indicate;
final bool authenticatedSignedWrites;
final bool extendedProperties;
final bool notifyEncryptionRequired;
final bool indicateEncryptionRequired;
CharacteristicProperties(
{this.broadcast = false,
this.read = false,
this.writeWithoutResponse = false,
this.write = false,
this.notify = false,
this.indicate = false,
this.authenticatedSignedWrites = false,
this.extendedProperties = false,
this.notifyEncryptionRequired = false,
this.indicateEncryptionRequired = false});
CharacteristicProperties.fromProto(protos.CharacteristicProperties p)
: broadcast = p.broadcast,
read = p.read,
writeWithoutResponse = p.writeWithoutResponse,
write = p.write,
notify = p.notify,
indicate = p.indicate,
authenticatedSignedWrites = p.authenticatedSignedWrites,
extendedProperties = p.extendedProperties,
notifyEncryptionRequired = p.notifyEncryptionRequired,
indicateEncryptionRequired = p.indicateEncryptionRequired;
}