-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
156 lines (116 loc) · 5.47 KB
/
example.js
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
/**
* @file Example to show how to use the mi_band_1a module
* @author David SALLE
* @version 0.1.3
*/
// Add module
var mb1a = require('./index');
//var mb1a = require('mi-band-1a');
/*
// Unit tests functions
var uid = mb1a.compute_uid('testy');
console.log('uid => 0x' + uid.toString(16));
var hash_code = mb1a.compute_hash_code('testy');
console.log('hash_code => 0x' + hash_code.toString(16));
var normalized_alias = mb1a.normalize('testy');
console.log('normalized_alias => 0x' + normalized_alias.toString('hex'));
var crc8 = mb1a.compute_crc8(normalized_alias);
console.log('crc8 => 0x' + crc8.toString(16));
var user_info = mb1a.generate_user_info('c80f10768f85', 2, 25, 175, 70, 'testy', 0, 5, 0);
console.log('user_info => 0x' + user_info.toString('hex'));
var activity_data = Buffer([1,17,4,7,9,42,1,4,0,4,0,5,0,0,5,0,0,5,0,0,5,0,0,1,17,4,7,9,46,1,4,0,0,0]);
var steps = mb1a.analyse_activity_data(activity_data);
console.log('steps => ' + steps);
*/
// Unit tests object
console.log('====== mi-band-1a module unit tests ======');
//process.setMaxListeners(0);
// Allowed wrists devices
var allowed_devices = ['c80f10768f85', 'c80f10768f79', 'c80f10768f16'];
// RSSI threshold to discover only close devices
var rssi_threshold = -70;
// Locker to handle one device only at a time
var device_in_progress = false;
var discover_in_progress = true;
// Called when a BLE device is dicovered
function onDiscover(ble_device) {
// Print device details
discover_in_progress = false;
console.log(' -> Discovered : ' + ble_device);
console.log(' + id : ' + ble_device._peripheral.id);
console.log(' + rssi : ' + ble_device._peripheral.rssi);
// Check if it is a mi band wrist and close enought
for (var i=0; i<allowed_devices.length; i++) {
if ( (allowed_devices[i] == ble_device._peripheral.id) && (ble_device._peripheral.rssi > rssi_threshold) && (device_in_progress == false) ) {
console.log(' -> Mi-band-1a found : ' + ble_device._peripheral.id);
device_in_progress = true;
discover_in_progress = false;
// What to do when we disconnect
ble_device.on('disconnect', function() {
console.log(' -> Disconnected ');
});
// Connect and setup with the Mi Band 1A
ble_device.connectAndSetUp(function(error) {
console.log(' -> Connected to ' + ble_device._peripheral.id);
// Subscribe to some notifications (NOTIFICATION & ACTIVITY_DATA)
ble_device.notifyActivityData(function() {
console.log(' -> Subscribe to ACTIVITY_DATA notifications');
});
// Initialize dialog
ble_device.initialize(function() {
console.log(' -> Initialize communication');
});
// Read device informations
ble_device.readDeviceInformations(function(error, data) {
console.log(' -> Read device informations');
});
// Authenticate
ble_device.authenticate(ble_device._peripheral.id, 2, 25, 175, 70, 'testy', 0, 5, 0, function() {
console.log(' -> Authenticate');
});
// Read date/time
ble_device.readDateTime(function(error, year, month, day, hour, minute, second) {
console.log(' -> Read date/time : ');
console.log(' + date/time : ' + day + '/' + month + '/' + year + ' at ' + hour + 'h' + minute + ':' + second);
});
// Read device informations
ble_device.readDeviceInformations(function(error, data) {
console.log(' -> Read device informations');
});
// Read battery
ble_device.readBattery(function(error, level, year, month, day, hour, minute, second, cycles, status) {
console.log(' -> Read battery : ');
console.log(' + battery level : ' + level + '%');
console.log(' + last time charged : ' + day + '/' + month + '/' + year + ' at ' + hour + 'h' + minute + ':' + second);
console.log(' + charge cycles : ' + cycles);
console.log(' + status (1=low, 2=charging, 3=full, 4=not charging): ' + status);
});
// Fetch activity data
ble_device.fetchActivityData(function() {
console.log(' -> Fetch activity data');
});
// Read steps
ble_device.readSteps(function(error, steps) {
console.log(' -> Read steps : ' + steps);
// Vibrate to indicate, data transfer is done
//ble_device.vibrate(function(error) {
// console.log(' -> Vibrate ');
//});
// Disconnect the device
ble_device.tryDisconnect(function(error) {
console.log(' -> Disconnect...' + error);
device_in_progress = false;
discover_in_progress = true;
delete ble_device;
});
});
});
}
}
}
// Start to discover all BLE devices every 10s
mb1a.discoverAll(onDiscover);
setInterval(function() {
mb1a.stopDiscoverAll(onDiscover);
mb1a.discoverAll(onDiscover);
}, 10000);