-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathcharacteristic.dart
More file actions
205 lines (182 loc) · 6.85 KB
/
Copy pathcharacteristic.dart
File metadata and controls
205 lines (182 loc) · 6.85 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
part of flutter_ble_lib;
abstract class _CharacteristicMetadata {
static const String uuid = "characteristicUuid";
static const String id = "id";
static const String isReadable = "isReadable";
static const String isWritableWithResponse = "isWritableWithResponse";
static const String isWritableWithoutResponse = "isWritableWithoutResponse";
static const String isNotifiable = "isNotifiable";
static const String isIndicatable = "isIndicatable";
static const String value = "value";
}
/// Representation of a single GATT Characteristic nested inside a [Service].
///
/// It contains a single value and any number of [Descriptor]s describing that
/// value. The properties of a characteristic determine how you can use
/// a characteristic’s value, and how you access the descriptors.
class Characteristic extends InternalCharacteristic {
/// The [Service] containing this characteristic.
Service service;
ManagerForCharacteristic _manager;
TransactionIdGenerator _transactionIdGenerator;
/// The UUID of this characteristic.
String uuid;
/// True if this characteristic can be read.
bool isReadable;
/// True if this characteristic can be written with resposne.
bool isWritableWithResponse;
/// True if this characteristic can be written without resposne.
bool isWritableWithoutResponse;
/// True if this characteristic can be monitored via notifications.
bool isNotifiable;
/// True if this characteristic can be monitored via indications.
bool isIndicatable;
Characteristic.fromJson(
Map<String, dynamic> jsonObject,
Service service,
ManagerForCharacteristic manager, {
TransactionIdGenerator transactionIdGenerator =
TransactionIdGenerator.INSTANCE,
}) : super(jsonObject[_CharacteristicMetadata.id]) {
_manager = manager;
_transactionIdGenerator = transactionIdGenerator;
this.service = service;
uuid = jsonObject[_CharacteristicMetadata.uuid];
isReadable = jsonObject[_CharacteristicMetadata.isReadable];
isWritableWithResponse =
jsonObject[_CharacteristicMetadata.isWritableWithResponse];
isWritableWithoutResponse =
jsonObject[_CharacteristicMetadata.isWritableWithoutResponse];
isNotifiable = jsonObject[_CharacteristicMetadata.isNotifiable];
isIndicatable = jsonObject[_CharacteristicMetadata.isIndicatable];
}
/// Reads the value of this characteristic.
///
/// The value can be read only if [isReadable] is `true`.
Future<Uint8List> read({String transactionId}) =>
_manager.readCharacteristicForIdentifier(
service.peripheral,
this,
transactionId ?? _transactionIdGenerator.getNextId(),
);
/// Writes to the value of this characteristic.
///
/// The value can be written only if [isWritableWithResponse] or
/// [isWritableWithoutResponse] is `true` and argument [withResponse] is
/// set accordingly.
Future<void> write(
Uint8List value,
bool withResponse, {
String transactionId,
}) =>
_manager.writeCharacteristicForIdentifier(
service.peripheral,
this,
value,
withResponse,
transactionId ?? _transactionIdGenerator.getNextId(),
);
/// Returns a [Stream] of notifications/indications emitted by this
/// characteristic.
///
/// Library chooses notifications over indications, if both are supported.
///
/// Subscribing to the returned object enables the notifications/indications
/// on the peripheral. Cancelling the last subscription disables the
/// notifications/indications on this characteristic.
Stream<Uint8List> monitor({String transactionId}) =>
_manager.monitorCharacteristicForIdentifier(
service.peripheral,
this,
transactionId ?? _transactionIdGenerator.getNextId(),
);
/// Returns a list of [Descriptor]s of this characteristic.
Future<List<Descriptor>> descriptors() =>
_manager.descriptorsForCharacteristic(this);
/// Reads the value of a [Descriptor] identified by [descriptorUuid].
Future<DescriptorWithValue> readDescriptor(
String descriptorUuid, {
String transactionId,
}) =>
_manager.readDescriptorForCharacteristic(
this,
descriptorUuid,
transactionId ?? _transactionIdGenerator.getNextId(),
);
/// Writes the [value] of a [Descriptor] identified by [descriptorUuid].
Future<Descriptor> writeDescriptor(
String descriptorUuid,
Uint8List value, {
String transactionId,
}) =>
_manager.writeDescriptorForCharacteristic(
this,
descriptorUuid,
value,
transactionId ?? _transactionIdGenerator.getNextId(),
);
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Characteristic &&
runtimeType == other.runtimeType &&
service == other.service &&
_manager == other._manager &&
uuid == other.uuid &&
isReadable == other.isReadable &&
isWritableWithResponse == other.isWritableWithResponse &&
isWritableWithoutResponse == other.isWritableWithoutResponse &&
isNotifiable == other.isNotifiable &&
isIndicatable == other.isIndicatable;
@override
int get hashCode =>
service.hashCode ^
_manager.hashCode ^
uuid.hashCode ^
isReadable.hashCode ^
isWritableWithResponse.hashCode ^
isWritableWithoutResponse.hashCode ^
isNotifiable.hashCode ^
isIndicatable.hashCode;
/// Returns a string representation of this characteristic in a format that
/// contains all its properties and [Service].
@override
String toString() {
return 'Characteristic{service: $service,'
' _manager: $_manager,'
' uuid: $uuid,'
' isReadable: $isReadable,'
' isWritableWithResponse: $isWritableWithResponse,'
' isWritableWithoutResponse: $isWritableWithoutResponse,'
' isNotifiable: $isNotifiable,'
' isIndicatable: $isIndicatable}';
}
}
/// [Characteristic] extended with [value] property.
///
/// This type is created to support chaining of operations on the characteristic
/// when it was first read from [Peripheral] or [Service].
class CharacteristicWithValue extends Characteristic with WithValue {
CharacteristicWithValue.fromJson(
Map<String, dynamic> jsonObject,
Service service,
ManagerForCharacteristic manager,
) : super.fromJson(jsonObject, service, manager) {
value = base64Decode(jsonObject[_CharacteristicMetadata.value]);
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
super == other &&
other is CharacteristicWithValue &&
value?.toString() == other.value?.toString() &&
runtimeType == other.runtimeType;
}
@override
int get hashCode => super.hashCode;
@override
String toString() {
return super.toString() +
' CharacteristicWithValue{value = ${value.toString()}';
}
}