forked from pauldemarco/flutter_blue
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbluetooth_characteristic.dart
133 lines (114 loc) · 4.12 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
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
// 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 bluetooth;
class BluetoothCharacteristicIdentifier {
final Uuid uuid;
final int instanceId;
final int _hashCode;
BluetoothCharacteristicIdentifier(Uuid uuid, int instanceId)
: uuid = uuid,
instanceId = instanceId,
_hashCode = _calcHashCode(uuid, instanceId);
factory BluetoothCharacteristicIdentifier.fromProto(
protos.BluetoothCharacteristicIdentifier p) =>
BluetoothCharacteristicIdentifier(Uuid.fromString(p.uuid), p.instanceId);
@override
String toString() {
return """"[UUID "$uuid", InstanceId "$instanceId"]""";
}
protos.BluetoothCharacteristicIdentifier get toProto {
return protos.BluetoothCharacteristicIdentifier.create()
..uuid = this.uuid.toString()
..instanceId = this.instanceId
;
}
static int _calcHashCode(Uuid uuid, int instanceId) {
final List<int> bytes = List.from(uuid.toList())..add(instanceId);
const equality = const ListEquality<int>();
return equality.hash(bytes);
}
int get hashCode => _hashCode;
operator ==(other) =>
other is BluetoothCharacteristicIdentifier &&
this.hashCode == other.hashCode;
}
class BluetoothCharacteristic {
final BluetoothCharacteristicIdentifier id;
final Uuid serviceUuid; // The service that this characteristic belongs to.
final Uuid
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.id,
@required this.serviceUuid,
this.secondaryServiceUuid,
@required this.descriptors,
@required this.properties});
BluetoothCharacteristic.fromProto(protos.BluetoothCharacteristic p)
: id = BluetoothCharacteristicIdentifier.fromProto(p.identifier),
serviceUuid = Uuid.fromString(p.serviceUuid),
secondaryServiceUuid = (p.secondaryServiceUuid.length > 0)
? Uuid.fromString(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;
}