@mmote/niimblue-node 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -33,7 +33,7 @@ Global (for cli usage):
33
33
  npm i -g @mmote/niimblue-node
34
34
  ```
35
35
 
36
- [node-gyp](https://www.npmjs.com/package/node-gyp) is required to install [noble](https://www.npmjs.com/package/@abandonware/noble) dependency.
36
+ [node-gyp](https://www.npmjs.com/package/node-gyp) is required to install [noble](https://www.npmjs.com/package/@mmote/niimblue-node) dependency.
37
37
  It requires working compiler installed on your system.
38
38
 
39
39
  Windows requirements:
@@ -10,7 +10,6 @@ export declare class NiimbotHeadlessBleClient extends NiimbotAbstractClient {
10
10
  constructor();
11
11
  /** Set device mac address or name for connect */
12
12
  setAddress(address: string): void;
13
- static waitAdapterReady(): Promise<void>;
14
13
  static scan(timeoutMs?: number): Promise<ScanItem[]>;
15
14
  private getDevice;
16
15
  private connectToDevice;
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.NiimbotHeadlessBleClient = void 0;
7
- const noble_1 = __importDefault(require("@abandonware/noble"));
7
+ const noble_1 = __importDefault(require("@stoprocent/noble"));
8
8
  const niimbluelib_1 = require("@mmote/niimbluelib");
9
9
  class NiimbotHeadlessBleClient extends niimbluelib_1.NiimbotAbstractClient {
10
10
  constructor() {
@@ -15,51 +15,21 @@ class NiimbotHeadlessBleClient extends niimbluelib_1.NiimbotAbstractClient {
15
15
  setAddress(address) {
16
16
  this.addr = address;
17
17
  }
18
- static async waitAdapterReady() {
19
- if (noble_1.default._state === "poweredOn") {
20
- return;
21
- }
22
- return new Promise((resolve, reject) => {
23
- let timer;
24
- noble_1.default.on("stateChange", async (state) => {
25
- clearTimeout(timer);
26
- if (state === "poweredOn") {
27
- resolve();
28
- }
29
- else {
30
- reject(new Error(`BLE state is ${state}`));
31
- }
32
- });
33
- timer = setTimeout(() => {
34
- reject(new Error("Can't init BLE"));
35
- }, 5000);
36
- });
37
- }
38
18
  static async scan(timeoutMs = 5000) {
39
- await NiimbotHeadlessBleClient.waitAdapterReady();
40
- return new Promise((resolve, reject) => {
41
- const peripherals = [];
42
- let timer;
43
- noble_1.default.on("discover", async (peripheral) => {
44
- peripherals.push({
45
- address: peripheral.address,
46
- name: peripheral.advertisement.localName || "unknown",
47
- });
19
+ const items = [];
20
+ await noble_1.default.waitForPoweredOnAsync(5000);
21
+ await noble_1.default.startScanningAsync([], false);
22
+ setTimeout(() => noble_1.default.stopScanningAsync(), timeoutMs ?? 5000);
23
+ for await (const peripheral of noble_1.default.discoverAsync()) {
24
+ items.push({
25
+ address: peripheral.address,
26
+ name: peripheral.advertisement.localName || "unknown",
48
27
  });
49
- noble_1.default.startScanning([], false, (error) => {
50
- if (error) {
51
- clearTimeout(timer);
52
- reject(error);
53
- }
54
- });
55
- timer = setTimeout(() => {
56
- noble_1.default.stopScanning();
57
- resolve(peripherals);
58
- }, timeoutMs ?? 5000);
59
- });
28
+ }
29
+ return items;
60
30
  }
61
31
  async getDevice(address, timeoutMs = 5000) {
62
- await NiimbotHeadlessBleClient.waitAdapterReady();
32
+ await noble_1.default.waitForPoweredOnAsync(5000);
63
33
  return new Promise((resolve, reject) => {
64
34
  let timer;
65
35
  noble_1.default.on("discover", async (peripheral) => {
@@ -105,11 +75,11 @@ class NiimbotHeadlessBleClient extends niimbluelib_1.NiimbotAbstractClient {
105
75
  this.device = undefined;
106
76
  this.channel = undefined;
107
77
  });
108
- channelCharacteristic.on("read", (data, isNotification) => {
78
+ channelCharacteristic.on("data", (data, isNotification) => {
109
79
  if (isNotification)
110
80
  this.processRawPacket(new Uint8Array(data));
111
81
  });
112
- channelCharacteristic.subscribeAsync();
82
+ await channelCharacteristic.subscribeAsync();
113
83
  this.channel = channelCharacteristic;
114
84
  this.device = periph;
115
85
  }
@@ -141,7 +111,6 @@ class NiimbotHeadlessBleClient extends niimbluelib_1.NiimbotAbstractClient {
141
111
  this.stopHeartbeat();
142
112
  if (this.device !== undefined) {
143
113
  await this.device.disconnectAsync();
144
- this.emit("disconnect", new niimbluelib_1.DisconnectEvent());
145
114
  }
146
115
  this.device = undefined;
147
116
  this.channel = undefined;
@@ -150,7 +119,7 @@ class NiimbotHeadlessBleClient extends niimbluelib_1.NiimbotAbstractClient {
150
119
  const send = async () => {
151
120
  if (!this.isConnected()) {
152
121
  this.disconnect();
153
- throw new Error("Disconnected");
122
+ throw new Error("Not connected");
154
123
  }
155
124
  await niimbluelib_1.Utils.sleep(this.packetIntervalMs);
156
125
  await this.channel.writeAsync(Buffer.from(data), true);
@@ -78,7 +78,18 @@ class NiimbotHeadlessSerialClient extends niimbluelib_1.NiimbotAbstractClient {
78
78
  }
79
79
  async disconnect() {
80
80
  this.stopHeartbeat();
81
- this.device?.close();
81
+ if (this.device) {
82
+ return new Promise((resolve, reject) => {
83
+ this.device.close((e) => {
84
+ if (e) {
85
+ reject(e);
86
+ }
87
+ else {
88
+ resolve();
89
+ }
90
+ });
91
+ });
92
+ }
82
93
  }
83
94
  isConnected() {
84
95
  return this.isOpen;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmote/niimblue-node",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "Headless clients for niimbluelib. Command line interface, simple REST server are also included.",
5
5
  "keywords": [
6
6
  "command-line",
@@ -38,9 +38,9 @@
38
38
  "typescript": "^5.4.5"
39
39
  },
40
40
  "dependencies": {
41
- "@abandonware/noble": "^1.9.2-26",
42
41
  "@commander-js/extra-typings": "^12.1.0",
43
42
  "@mmote/niimbluelib": "0.0.1-alpha.32",
43
+ "@stoprocent/noble": "^2.3.10",
44
44
  "async-mutex": "^0.5.0",
45
45
  "commander": "^12.1.0",
46
46
  "serialport": "^12.0.0",