Skip to content

Commit fc95738

Browse files
committed
Released v0.0.5
1 parent 002d8be commit fc95738

File tree

3 files changed

+69
-37
lines changed

3 files changed

+69
-37
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ The `battery` is *experimental* for now. I'm not sure whether the value is corre
750750
---------------------------------------
751751
## <a id="Release-Note">Release Note</a>
752752

753+
* v0.0.5 (2020-02-19)
754+
* Improved the stability of discovering the BLE characteristics.
753755
* v0.0.4 (2020-02-11)
754756
* Fixed the bug that temperature value lower than 0 degC could not be handled. (Thanks to [@musimasami](https://github.com/futomi/node-switchbot/issues/2))
755757
* v0.0.3 (2020-02-10)

lib/switchbot-device.js

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2019-2020, Futomi Hatano, All rights reserved.
55
* Released under the MIT license
6-
* Date: 2020-02-10
6+
* Date: 2020-02-19
77
* ---------------------------------------------------------------- */
88
'use strict';
99
const parameterChecker = require('./parameter-checker.js');
@@ -108,6 +108,7 @@ class SwitchbotDevice {
108108
this._peripheral.once('connect', () => {
109109
this._onconnect();
110110
});
111+
111112
this._peripheral.once('disconnect', () => {
112113
this._chars = null;
113114
this._peripheral.removeAllListeners();
@@ -121,7 +122,7 @@ class SwitchbotDevice {
121122
reject(error);
122123
return;
123124
}
124-
this._discoverCharacteristics().then((chars) => {
125+
this._getCharacteristics().then((chars) => {
125126
this._chars = chars;
126127
return this._subscribe();
127128
}).then(() => {
@@ -134,12 +135,12 @@ class SwitchbotDevice {
134135
});
135136
}
136137

137-
_discoverCharacteristics() {
138+
_getCharacteristics() {
138139
return new Promise((resolve, reject) => {
139140
// Set timeout timer
140141
let timer = setTimeout(() => {
141142
this._ondisconnect_internal = () => { };
142-
this._peripheral.disconnect();
143+
timer = null;
143144
reject(new Error('Failed to discover services and characteristics: TIMEOUT'));
144145
}, 5000);
145146

@@ -148,60 +149,89 @@ class SwitchbotDevice {
148149
if (timer) {
149150
clearTimeout(timer);
150151
timer = null;
152+
this._ondisconnect_internal = () => { };
151153
}
152154
reject(new Error('Failed to discover services and characteristics: DISCONNECTED'));
153155
};
154156

155157
// Discover services and characteristics
156-
this._peripheral.discoverAllServicesAndCharacteristics((error, services, chars) => {
158+
(async () => {
159+
let service_list = await this._discoverServices();
160+
if (!timer) {
161+
throw new Error('');
162+
}
163+
164+
let chars = {
165+
write: null,
166+
notify: null,
167+
device: null
168+
};
169+
170+
for (let service of service_list) {
171+
let char_list = await this._discoverCharacteristics(service);
172+
for (let char of char_list) {
173+
if (char.uuid === this._CHAR_UUID_WRITE) {
174+
chars.write = char;
175+
} else if (char.uuid === this._CHAR_UUID_NOTIFY) {
176+
chars.notify = char;
177+
} else if (char.uuid === this._CHAR_UUID_DEVICE) {
178+
// Some models of Bot don't seem to support this characteristic UUID
179+
chars.device = char;
180+
}
181+
}
182+
}
183+
184+
if (chars.write && chars.notify) {
185+
resolve(chars);
186+
} else {
187+
reject(new Error('No characteristic was found.'));
188+
}
189+
190+
})().catch((error) => {
157191
if (timer) {
158192
clearTimeout(timer);
159193
timer = null;
194+
this._ondisconnect_internal = () => { };
195+
reject(error);
196+
} else {
197+
// Do nothing
160198
}
161-
this._ondisconnect_internal = () => { };
199+
});
200+
});
201+
}
162202

203+
_discoverServices() {
204+
return new Promise((resolve, reject) => {
205+
this._peripheral.discoverServices([], (error, service_list) => {
163206
if (error) {
164207
reject(error);
165208
return;
166209
}
167210

168-
// Check the primary service UUID
169211
let service = null;
170-
services.forEach((s) => {
212+
for (let s of service_list) {
171213
if (s.uuid === this._SERV_UUID_PRIMARY) {
172214
service = s;
215+
break;
173216
}
174-
});
175-
if (!service) {
176-
reject(new Error('No service was found'));
177-
return;
178217
}
179-
180-
// Check the characteristics
181-
let char_write = null;
182-
let char_notify = null;
183-
let char_device = null;
184-
185-
chars.forEach((c) => {
186-
if (c.uuid === this._CHAR_UUID_WRITE) {
187-
char_write = c;
188-
} else if (c.uuid === this._CHAR_UUID_NOTIFY) {
189-
char_notify = c;
190-
} else if (c.uuid === this._CHAR_UUID_DEVICE) {
191-
// Some models of Bot don't seem to support this characteristic UUID
192-
char_device = c;
193-
}
194-
});
195-
if (!(char_write && char_notify)) {
196-
reject(new Error('No characteristic was found.'));
197-
return;
218+
if (service) {
219+
resolve(service_list);
220+
} else {
221+
reject(new Error('No service was found.'));
198222
}
223+
});
224+
});
225+
}
199226

200-
resolve({
201-
write: char_write,
202-
notify: char_notify,
203-
device: char_device
204-
});
227+
_discoverCharacteristics(service) {
228+
return new Promise((resolve, reject) => {
229+
service.discoverCharacteristics([], (error, char_list) => {
230+
if (error) {
231+
reject(error);
232+
} else {
233+
resolve(char_list);
234+
}
205235
});
206236
});
207237
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-switchbot",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "The node-switchbot is a Node.js module which allows you to move your Switchbot (Bot)'s arm and to monitor the temperature/humidity from SwitchBot Thermometer & Hygrometer (Meter).",
55
"main": "./lib/switchbot.js",
66
"files": [

0 commit comments

Comments
 (0)