-
Notifications
You must be signed in to change notification settings - Fork 37
/
sensor_tag.dart
179 lines (164 loc) · 6.55 KB
/
sensor_tag.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
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
import 'dart:async';
import 'dart:math';
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:blemulator/blemulator.dart';
// Simplified simulation of Texas Instruments CC2541 SensorTag
// http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf
class SensorTag extends SimulatedPeripheral {
SensorTag(
{String id = '4B:99:4C:34:DE:77',
String name = 'SensorTag',
String localName = 'SensorTag'})
: super(
name: name,
id: id,
advertisementInterval: Duration(milliseconds: 800),
services: [
//IR Temperature service implemented according to docs
TemperatureService(
uuid: 'F000AA00-0451-4000-B000-000000000000',
isAdvertised: true,
convenienceName: 'Temperature service'),
//Simplified accelerometer service
SimulatedService(
uuid: 'F000AA10-0451-4000-B000-000000000000',
isAdvertised: true,
characteristics: [
SimulatedCharacteristic(
uuid: 'F000AA11-0451-4000-B000-000000000000',
value: Uint8List.fromList([0, 0]),
convenienceName: 'Accelerometer Config',
isWritableWithResponse: false,
isWritableWithoutResponse: false,
isNotifiable: true),
SimulatedCharacteristic(
uuid: 'F000AA12-0451-4000-B000-000000000000',
value: Uint8List.fromList([2]),
convenienceName: 'Accelerometer Config'),
SimulatedCharacteristic(
uuid: 'F000AA13-0451-4000-B000-000000000000',
value: Uint8List.fromList([30]),
convenienceName: 'Accelerometer Period'),
],
convenienceName: 'Accelerometer Service')
]) {
scanInfo.localName = localName;
scanInfo.manufacturerData = Uint8List.fromList([0, 1, 2, 255]);
}
@override
Future<bool> onConnectRequest() async {
await Future.delayed(Duration(milliseconds: 200));
return super.onConnectRequest();
}
@override
Future<void> onDiscoveryRequest() async {
await Future.delayed(Duration(milliseconds: 500));
return super.onDiscoveryRequest();
}
}
//IR Temperature Service of sensor tag
// According to documentation the sensor can be turned on and off by
// writing to IR Temperature Config characteristic, the period of
// notifications is 10 times the value of IR Temperature Period characteristic
// and the value of the temperature can be retrieved from IR Temperature Data
// characteristic through reading or notifications
class TemperatureService extends SimulatedService {
static const String _temperatureDataUuid =
'F000AA01-0451-4000-B000-000000000000';
static const String _temperatureConfigUuid =
'F000AA02-0451-4000-B000-000000000000';
static const String _temperaturePeriodUuid =
'F000AA03-0451-4000-B000-000000000000';
bool _readingTemperature = false;
TemperatureService(
{@required String uuid,
@required bool isAdvertised,
String convenienceName})
: super(
uuid: uuid,
isAdvertised: isAdvertised,
characteristics: [
SimulatedCharacteristic(
uuid: _temperatureDataUuid,
value: Uint8List.fromList([0, 0, 0, 0]),
convenienceName: 'IR Temperature Data',
isNotifiable: true,
descriptors: [
SimulatedDescriptor(
uuid: '00002901-0000-1000-8000-00805f9b34fb',
value: Uint8List.fromList([0]),
convenienceName:
'Client characteristic configuration',
),
SimulatedDescriptor(
uuid: '00002902-0000-1000-8000-00805f9b34fb',
value: Uint8List.fromList([0]),
writable: false,
convenienceName:
'Characteristic user description',
),
],
),
BooleanCharacteristic(
uuid: _temperatureConfigUuid,
initialValue: false,
convenienceName: 'IR Temperature Config',
),
SimulatedCharacteristic(
uuid: _temperaturePeriodUuid,
value: Uint8List.fromList([50]),
convenienceName: 'IR Temperature Period'),
],
convenienceName: convenienceName) {
characteristicByUuid(_temperatureConfigUuid).monitor().listen((value) {
_readingTemperature = value[0] == 1;
if (_readingTemperature) {
var temperatureDataCharacteristic =
characteristicByUuid(_temperatureDataUuid);
temperatureDataCharacteristic.write(
Uint8List.fromList([0, 0, 100, Random().nextInt(255)]),
sendNotification: false,
);
}
});
_emitTemperature();
}
void _emitTemperature() async {
while (true) {
var delayBytes =
await characteristicByUuid(_temperaturePeriodUuid).read();
var delay = delayBytes[0] * 10;
await Future.delayed(Duration(milliseconds: delay));
var temperatureDataCharacteristic =
characteristicByUuid(_temperatureDataUuid);
if (temperatureDataCharacteristic.isNotifying) {
if (_readingTemperature) {
await temperatureDataCharacteristic
.write(Uint8List.fromList([0, 0, 100, Random().nextInt(255)]));
} else {
await temperatureDataCharacteristic.write(Uint8List.fromList([0, 0, 0, 0]));
}
}
}
}
}
class BooleanCharacteristic extends SimulatedCharacteristic {
BooleanCharacteristic(
{@required uuid, @required bool initialValue, String convenienceName})
: super(
uuid: uuid,
value: Uint8List.fromList([initialValue ? 1 : 0]),
convenienceName: convenienceName);
@override
Future<void> write(Uint8List value, {bool sendNotification = true}) {
var valueAsInt = value[0];
if (valueAsInt != 0 && valueAsInt != 1) {
return Future.error(SimulatedBleError(
BleErrorCode.CharacteristicWriteFailed, 'Unsupported value'));
} else {
return super.write(value); //this propagates value through the blemulator,
// allowing you to react to changes done to this characteristic
}
}
}