@hangtime/grip-connect 0.0.6 → 0.0.7
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 +5 -5
- package/build/connect.d.ts +1 -1
- package/build/connect.js +53 -30
- package/build/read.d.ts +1 -1
- package/build/read.js +7 -3
- package/package.json +1 -1
- package/src/characteristic.d.ts +9 -0
- package/src/characteristic.js +15 -0
- package/src/connect.d.ts +1 -1
- package/src/connect.js +53 -30
- package/src/connect.ts +65 -38
- package/src/devices/entralpi.d.ts +2 -0
- package/src/devices/entralpi.js +52 -0
- package/src/devices/index.d.ts +3 -0
- package/src/devices/index.js +3 -0
- package/src/devices/moterboard.d.ts +2 -0
- package/src/devices/moterboard.js +79 -0
- package/src/devices/tindeq.d.ts +17 -0
- package/src/devices/tindeq.js +37 -0
- package/src/devices/types.d.ts +20 -0
- package/src/devices/types.js +1 -0
- package/src/disconnect.d.ts +6 -0
- package/src/disconnect.js +11 -0
- package/src/index.d.ts +6 -0
- package/src/index.js +6 -0
- package/src/notify.d.ts +4 -0
- package/src/notify.js +6 -0
- package/src/read.d.ts +6 -0
- package/src/read.js +44 -0
- package/src/read.ts +7 -3
- package/src/write.d.ts +7 -0
- package/src/write.js +32 -0
package/README.md
CHANGED
|
@@ -56,13 +56,13 @@ motherboardButton.addEventListener("click", () => {
|
|
|
56
56
|
})
|
|
57
57
|
|
|
58
58
|
// read battery + device info
|
|
59
|
-
await read(Motherboard, "battery", "level")
|
|
60
|
-
await read(Motherboard, "device", "manufacturer")
|
|
61
|
-
await read(Motherboard, "device", "hardware")
|
|
62
|
-
await read(Motherboard, "device", "firmware")
|
|
59
|
+
await read(Motherboard, "battery", "level", 1000)
|
|
60
|
+
await read(Motherboard, "device", "manufacturer", 1000)
|
|
61
|
+
await read(Motherboard, "device", "hardware", 1000)
|
|
62
|
+
await read(Motherboard, "device", "firmware", 1000)
|
|
63
63
|
|
|
64
64
|
// Calibrate?
|
|
65
|
-
await write(Motherboard, "uart", "tx", "C",
|
|
65
|
+
await write(Motherboard, "uart", "tx", "C", 10000)
|
|
66
66
|
|
|
67
67
|
// Read stream?
|
|
68
68
|
await write(Motherboard, "unknown", "01", "1", 2500)
|
package/build/connect.d.ts
CHANGED
package/build/connect.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { notifyCallback } from "./notify";
|
|
2
|
+
let server;
|
|
2
3
|
/**
|
|
3
4
|
* onDisconnected
|
|
4
5
|
* @param board
|
|
@@ -75,6 +76,50 @@ const handleNotifications = (event, board) => {
|
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
};
|
|
79
|
+
/**
|
|
80
|
+
* onConnected
|
|
81
|
+
* @param event
|
|
82
|
+
* @param board
|
|
83
|
+
*/
|
|
84
|
+
const onConnected = async (board, onSuccess) => {
|
|
85
|
+
try {
|
|
86
|
+
const services = await server?.getPrimaryServices();
|
|
87
|
+
if (!services || services.length === 0) {
|
|
88
|
+
console.error("No services found");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
for (const service of services) {
|
|
92
|
+
const matchingService = board.services.find((boardService) => boardService.uuid === service.uuid);
|
|
93
|
+
if (matchingService) {
|
|
94
|
+
// Android bug: Introduce a delay before getting characteristics
|
|
95
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
96
|
+
const characteristics = await service.getCharacteristics();
|
|
97
|
+
for (const characteristic of matchingService.characteristics) {
|
|
98
|
+
const matchingCharacteristic = characteristics.find((char) => char.uuid === characteristic.uuid);
|
|
99
|
+
if (matchingCharacteristic) {
|
|
100
|
+
const element = matchingService.characteristics.find((char) => char.uuid === matchingCharacteristic.uuid);
|
|
101
|
+
if (element) {
|
|
102
|
+
element.characteristic = matchingCharacteristic;
|
|
103
|
+
// notify
|
|
104
|
+
if (element.id === "rx") {
|
|
105
|
+
matchingCharacteristic.startNotifications();
|
|
106
|
+
matchingCharacteristic.addEventListener("characteristicvaluechanged", (event) => handleNotifications(event, board));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
console.warn(`Characteristic ${characteristic.uuid} not found in service ${service.uuid}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// Call the onSuccess callback after successful connection and setup
|
|
117
|
+
onSuccess();
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
console.error(error);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
78
123
|
/**
|
|
79
124
|
* Return all service UUIDs
|
|
80
125
|
* @param device
|
|
@@ -83,7 +128,7 @@ function getAllServiceUUIDs(device) {
|
|
|
83
128
|
return device.services.map((service) => service.uuid);
|
|
84
129
|
}
|
|
85
130
|
/**
|
|
86
|
-
*
|
|
131
|
+
* Connect to the BluetoothDevice
|
|
87
132
|
* @param device
|
|
88
133
|
* @param onSuccess
|
|
89
134
|
*/
|
|
@@ -108,40 +153,18 @@ export const connect = async (board, onSuccess) => {
|
|
|
108
153
|
}
|
|
109
154
|
const device = await navigator.bluetooth.requestDevice({
|
|
110
155
|
filters: filters,
|
|
111
|
-
optionalServices: deviceServices
|
|
156
|
+
optionalServices: deviceServices
|
|
112
157
|
});
|
|
113
158
|
board.device = device;
|
|
114
|
-
device.
|
|
115
|
-
|
|
116
|
-
const services = await server?.getPrimaryServices();
|
|
117
|
-
if (!services || services.length === 0) {
|
|
118
|
-
console.error("No services found");
|
|
159
|
+
if (!board.device.gatt) {
|
|
160
|
+
console.error("GATT is not available on this device");
|
|
119
161
|
return;
|
|
120
162
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
for (const characteristic of matchingService.characteristics) {
|
|
126
|
-
const matchingCharacteristic = characteristics.find((char) => char.uuid === characteristic.uuid);
|
|
127
|
-
if (matchingCharacteristic) {
|
|
128
|
-
const element = matchingService.characteristics.find((char) => char.uuid === matchingCharacteristic.uuid);
|
|
129
|
-
if (element) {
|
|
130
|
-
element.characteristic = matchingCharacteristic;
|
|
131
|
-
// notify
|
|
132
|
-
if (element.id === "rx") {
|
|
133
|
-
matchingCharacteristic.startNotifications();
|
|
134
|
-
matchingCharacteristic.addEventListener("characteristicvaluechanged", (event) => handleNotifications(event, board));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
console.warn(`Characteristic ${characteristic.uuid} not found in service ${service.uuid}`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
163
|
+
server = await board.device?.gatt?.connect();
|
|
164
|
+
board.device.addEventListener("gattserverdisconnected", (event) => onDisconnected(event, board));
|
|
165
|
+
if (server.connected) {
|
|
166
|
+
await onConnected(board, onSuccess);
|
|
143
167
|
}
|
|
144
|
-
onSuccess();
|
|
145
168
|
}
|
|
146
169
|
catch (error) {
|
|
147
170
|
console.error(error);
|
package/build/read.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ import { Device } from "./devices/types";
|
|
|
3
3
|
* read
|
|
4
4
|
* @param characteristic
|
|
5
5
|
*/
|
|
6
|
-
export declare const read: (board: Device, serviceId: string, characteristicId: string) => Promise<void>;
|
|
6
|
+
export declare const read: (board: Device, serviceId: string, characteristicId: string, duration?: number) => Promise<void>;
|
package/build/read.js
CHANGED
|
@@ -4,7 +4,7 @@ import { getCharacteristic } from "./characteristic";
|
|
|
4
4
|
* read
|
|
5
5
|
* @param characteristic
|
|
6
6
|
*/
|
|
7
|
-
export const read = (board, serviceId, characteristicId) => {
|
|
7
|
+
export const read = (board, serviceId, characteristicId, duration = 0) => {
|
|
8
8
|
return new Promise((resolve, reject) => {
|
|
9
9
|
if (board.device?.gatt?.connected) {
|
|
10
10
|
const characteristic = getCharacteristic(board, serviceId, characteristicId);
|
|
@@ -22,8 +22,12 @@ export const read = (board, serviceId, characteristicId) => {
|
|
|
22
22
|
decodedValue = decoder.decode(value);
|
|
23
23
|
break;
|
|
24
24
|
}
|
|
25
|
-
notifyCallback
|
|
26
|
-
|
|
25
|
+
if (notifyCallback) {
|
|
26
|
+
notifyCallback({ uuid: characteristic.uuid, value: decodedValue });
|
|
27
|
+
}
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
resolve();
|
|
30
|
+
}, duration);
|
|
27
31
|
})
|
|
28
32
|
.catch((error) => {
|
|
29
33
|
reject(error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hangtime/grip-connect",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "A client that can establish connections with various Force-Sensing Hangboards/Plates used by climbers for strength measurement. Examples of such hangboards include the Motherboard, Climbro, SmartBoard, Entralpi or Tindeq Progressor",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="web-bluetooth" />
|
|
2
|
+
import { Device } from "./devices/types";
|
|
3
|
+
/**
|
|
4
|
+
* getCharacteristic
|
|
5
|
+
* @param board
|
|
6
|
+
* @param serviceId
|
|
7
|
+
* @param characteristicId
|
|
8
|
+
*/
|
|
9
|
+
export declare const getCharacteristic: (board: Device, serviceId: string, characteristicId: string) => BluetoothRemoteGATTCharacteristic | undefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* getCharacteristic
|
|
3
|
+
* @param board
|
|
4
|
+
* @param serviceId
|
|
5
|
+
* @param characteristicId
|
|
6
|
+
*/
|
|
7
|
+
export const getCharacteristic = (board, serviceId, characteristicId) => {
|
|
8
|
+
const boardService = board.services.find((service) => service.id === serviceId);
|
|
9
|
+
if (boardService) {
|
|
10
|
+
const boardCharacteristic = boardService.characteristics.find((characteristic) => characteristic.id === characteristicId);
|
|
11
|
+
if (boardCharacteristic) {
|
|
12
|
+
return boardCharacteristic.characteristic;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
package/src/connect.d.ts
CHANGED
package/src/connect.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { notifyCallback } from "./notify";
|
|
2
|
+
let server;
|
|
2
3
|
/**
|
|
3
4
|
* onDisconnected
|
|
4
5
|
* @param board
|
|
@@ -75,6 +76,50 @@ const handleNotifications = (event, board) => {
|
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
};
|
|
79
|
+
/**
|
|
80
|
+
* onConnected
|
|
81
|
+
* @param event
|
|
82
|
+
* @param board
|
|
83
|
+
*/
|
|
84
|
+
const onConnected = async (board, onSuccess) => {
|
|
85
|
+
try {
|
|
86
|
+
const services = await server?.getPrimaryServices();
|
|
87
|
+
if (!services || services.length === 0) {
|
|
88
|
+
console.error("No services found");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
for (const service of services) {
|
|
92
|
+
const matchingService = board.services.find((boardService) => boardService.uuid === service.uuid);
|
|
93
|
+
if (matchingService) {
|
|
94
|
+
// Android bug: Introduce a delay before getting characteristics
|
|
95
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
96
|
+
const characteristics = await service.getCharacteristics();
|
|
97
|
+
for (const characteristic of matchingService.characteristics) {
|
|
98
|
+
const matchingCharacteristic = characteristics.find((char) => char.uuid === characteristic.uuid);
|
|
99
|
+
if (matchingCharacteristic) {
|
|
100
|
+
const element = matchingService.characteristics.find((char) => char.uuid === matchingCharacteristic.uuid);
|
|
101
|
+
if (element) {
|
|
102
|
+
element.characteristic = matchingCharacteristic;
|
|
103
|
+
// notify
|
|
104
|
+
if (element.id === "rx") {
|
|
105
|
+
matchingCharacteristic.startNotifications();
|
|
106
|
+
matchingCharacteristic.addEventListener("characteristicvaluechanged", (event) => handleNotifications(event, board));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
console.warn(`Characteristic ${characteristic.uuid} not found in service ${service.uuid}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// Call the onSuccess callback after successful connection and setup
|
|
117
|
+
onSuccess();
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
console.error(error);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
78
123
|
/**
|
|
79
124
|
* Return all service UUIDs
|
|
80
125
|
* @param device
|
|
@@ -83,7 +128,7 @@ function getAllServiceUUIDs(device) {
|
|
|
83
128
|
return device.services.map((service) => service.uuid);
|
|
84
129
|
}
|
|
85
130
|
/**
|
|
86
|
-
*
|
|
131
|
+
* Connect to the BluetoothDevice
|
|
87
132
|
* @param device
|
|
88
133
|
* @param onSuccess
|
|
89
134
|
*/
|
|
@@ -108,40 +153,18 @@ export const connect = async (board, onSuccess) => {
|
|
|
108
153
|
}
|
|
109
154
|
const device = await navigator.bluetooth.requestDevice({
|
|
110
155
|
filters: filters,
|
|
111
|
-
optionalServices: deviceServices
|
|
156
|
+
optionalServices: deviceServices
|
|
112
157
|
});
|
|
113
158
|
board.device = device;
|
|
114
|
-
device.
|
|
115
|
-
|
|
116
|
-
const services = await server?.getPrimaryServices();
|
|
117
|
-
if (!services || services.length === 0) {
|
|
118
|
-
console.error("No services found");
|
|
159
|
+
if (!board.device.gatt) {
|
|
160
|
+
console.error("GATT is not available on this device");
|
|
119
161
|
return;
|
|
120
162
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
for (const characteristic of matchingService.characteristics) {
|
|
126
|
-
const matchingCharacteristic = characteristics.find((char) => char.uuid === characteristic.uuid);
|
|
127
|
-
if (matchingCharacteristic) {
|
|
128
|
-
const element = matchingService.characteristics.find((char) => char.uuid === matchingCharacteristic.uuid);
|
|
129
|
-
if (element) {
|
|
130
|
-
element.characteristic = matchingCharacteristic;
|
|
131
|
-
// notify
|
|
132
|
-
if (element.id === "rx") {
|
|
133
|
-
matchingCharacteristic.startNotifications();
|
|
134
|
-
matchingCharacteristic.addEventListener("characteristicvaluechanged", (event) => handleNotifications(event, board));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
console.warn(`Characteristic ${characteristic.uuid} not found in service ${service.uuid}`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
163
|
+
server = await board.device?.gatt?.connect();
|
|
164
|
+
board.device.addEventListener("gattserverdisconnected", (event) => onDisconnected(event, board));
|
|
165
|
+
if (server.connected) {
|
|
166
|
+
await onConnected(board, onSuccess);
|
|
143
167
|
}
|
|
144
|
-
onSuccess();
|
|
145
168
|
}
|
|
146
169
|
catch (error) {
|
|
147
170
|
console.error(error);
|
package/src/connect.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Device } from "./devices/types"
|
|
2
2
|
import { notifyCallback } from "./notify"
|
|
3
3
|
|
|
4
|
+
let server: BluetoothRemoteGATTServer
|
|
5
|
+
|
|
4
6
|
/**
|
|
5
7
|
* onDisconnected
|
|
6
8
|
* @param board
|
|
@@ -48,12 +50,12 @@ const handleNotifications = (event: Event, board: Device): void => {
|
|
|
48
50
|
"pressure2",
|
|
49
51
|
"right",
|
|
50
52
|
]
|
|
51
|
-
const dataObject: { [key: string]: number } = {}
|
|
53
|
+
const dataObject: { [key: string]: number } = {}
|
|
52
54
|
|
|
53
55
|
if (parsedDecimalArray) {
|
|
54
56
|
elementKeys.forEach((key: string, index: number) => {
|
|
55
|
-
dataObject[key] = parsedDecimalArray[index]
|
|
56
|
-
})
|
|
57
|
+
dataObject[key] = parsedDecimalArray[index]
|
|
58
|
+
})
|
|
57
59
|
}
|
|
58
60
|
if (notifyCallback) {
|
|
59
61
|
notifyCallback({ uuid: characteristic.uuid, value: dataObject })
|
|
@@ -76,6 +78,58 @@ const handleNotifications = (event: Event, board: Device): void => {
|
|
|
76
78
|
}
|
|
77
79
|
}
|
|
78
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* onConnected
|
|
83
|
+
* @param event
|
|
84
|
+
* @param board
|
|
85
|
+
*/
|
|
86
|
+
const onConnected = async (board: Device, onSuccess: () => void): Promise<void> => {
|
|
87
|
+
try {
|
|
88
|
+
const services = await server?.getPrimaryServices()
|
|
89
|
+
|
|
90
|
+
if (!services || services.length === 0) {
|
|
91
|
+
console.error("No services found")
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const service of services) {
|
|
96
|
+
const matchingService = board.services.find((boardService) => boardService.uuid === service.uuid)
|
|
97
|
+
|
|
98
|
+
if (matchingService) {
|
|
99
|
+
// Android bug: Introduce a delay before getting characteristics
|
|
100
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
101
|
+
|
|
102
|
+
const characteristics = await service.getCharacteristics()
|
|
103
|
+
|
|
104
|
+
for (const characteristic of matchingService.characteristics) {
|
|
105
|
+
const matchingCharacteristic = characteristics.find((char) => char.uuid === characteristic.uuid)
|
|
106
|
+
|
|
107
|
+
if (matchingCharacteristic) {
|
|
108
|
+
const element = matchingService.characteristics.find((char) => char.uuid === matchingCharacteristic.uuid)
|
|
109
|
+
if (element) {
|
|
110
|
+
element.characteristic = matchingCharacteristic
|
|
111
|
+
|
|
112
|
+
// notify
|
|
113
|
+
if (element.id === "rx") {
|
|
114
|
+
matchingCharacteristic.startNotifications()
|
|
115
|
+
matchingCharacteristic.addEventListener("characteristicvaluechanged", (event) =>
|
|
116
|
+
handleNotifications(event, board),
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
console.warn(`Characteristic ${characteristic.uuid} not found in service ${service.uuid}`)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Call the onSuccess callback after successful connection and setup
|
|
128
|
+
onSuccess()
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error(error)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
79
133
|
/**
|
|
80
134
|
* Return all service UUIDs
|
|
81
135
|
* @param device
|
|
@@ -84,7 +138,7 @@ function getAllServiceUUIDs(device: Device) {
|
|
|
84
138
|
return device.services.map((service) => service.uuid)
|
|
85
139
|
}
|
|
86
140
|
/**
|
|
87
|
-
*
|
|
141
|
+
* Connect to the BluetoothDevice
|
|
88
142
|
* @param device
|
|
89
143
|
* @param onSuccess
|
|
90
144
|
*/
|
|
@@ -112,50 +166,23 @@ export const connect = async (board: Device, onSuccess: () => void): Promise<voi
|
|
|
112
166
|
|
|
113
167
|
const device = await navigator.bluetooth.requestDevice({
|
|
114
168
|
filters: filters,
|
|
115
|
-
optionalServices: deviceServices
|
|
169
|
+
optionalServices: deviceServices
|
|
116
170
|
})
|
|
117
171
|
|
|
118
172
|
board.device = device
|
|
119
173
|
|
|
120
|
-
device.
|
|
121
|
-
|
|
122
|
-
const server = await device.gatt?.connect()
|
|
123
|
-
const services = await server?.getPrimaryServices()
|
|
124
|
-
|
|
125
|
-
if (!services || services.length === 0) {
|
|
126
|
-
console.error("No services found")
|
|
174
|
+
if (!board.device.gatt) {
|
|
175
|
+
console.error("GATT is not available on this device")
|
|
127
176
|
return
|
|
128
177
|
}
|
|
129
178
|
|
|
130
|
-
|
|
131
|
-
const matchingService = board.services.find((boardService) => boardService.uuid === service.uuid)
|
|
132
|
-
|
|
133
|
-
if (matchingService) {
|
|
134
|
-
const characteristics = await service.getCharacteristics()
|
|
179
|
+
server = await board.device?.gatt?.connect()
|
|
135
180
|
|
|
136
|
-
|
|
137
|
-
const matchingCharacteristic = characteristics.find((char) => char.uuid === characteristic.uuid)
|
|
181
|
+
board.device.addEventListener("gattserverdisconnected", (event) => onDisconnected(event, board))
|
|
138
182
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (element) {
|
|
142
|
-
element.characteristic = matchingCharacteristic
|
|
143
|
-
|
|
144
|
-
// notify
|
|
145
|
-
if (element.id === "rx") {
|
|
146
|
-
matchingCharacteristic.startNotifications()
|
|
147
|
-
matchingCharacteristic.addEventListener("characteristicvaluechanged", (event) =>
|
|
148
|
-
handleNotifications(event, board),
|
|
149
|
-
)
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
} else {
|
|
153
|
-
console.warn(`Characteristic ${characteristic.uuid} not found in service ${service.uuid}`)
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
183
|
+
if (server.connected) {
|
|
184
|
+
await onConnected(board, onSuccess);
|
|
157
185
|
}
|
|
158
|
-
onSuccess()
|
|
159
186
|
} catch (error) {
|
|
160
187
|
console.error(error)
|
|
161
188
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export const Entralpi = {
|
|
2
|
+
name: "ENTRALPI",
|
|
3
|
+
services: [
|
|
4
|
+
{
|
|
5
|
+
name: "Device Information",
|
|
6
|
+
id: "device",
|
|
7
|
+
uuid: "0000180a-0000-1000-8000-00805f9b34fb",
|
|
8
|
+
characteristics: [],
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
name: "Battery Service",
|
|
12
|
+
id: "battery",
|
|
13
|
+
uuid: "0000180f-0000-1000-8000-00805f9b34fb",
|
|
14
|
+
characteristics: [],
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: "Generic Attribute",
|
|
18
|
+
id: "attribute",
|
|
19
|
+
uuid: "00001801-0000-1000-8000-00805f9b34fb",
|
|
20
|
+
characteristics: [],
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "UART ISSC Transparent Service",
|
|
24
|
+
id: "uart",
|
|
25
|
+
uuid: "0000fff0-0000-1000-8000-00805f9b34fb",
|
|
26
|
+
characteristics: [
|
|
27
|
+
{
|
|
28
|
+
name: "TX",
|
|
29
|
+
id: "tx",
|
|
30
|
+
uuid: "0000fff5-0000-1000-8000-00805f9b34fb",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "RX",
|
|
34
|
+
id: "rx",
|
|
35
|
+
uuid: "0000fff4-0000-1000-8000-00805f9b34fb",
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: "Weight Scale",
|
|
41
|
+
id: "weight",
|
|
42
|
+
uuid: "0000181d-0000-1000-8000-00805f9b34fb",
|
|
43
|
+
characteristics: [],
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: "Generic Access",
|
|
47
|
+
id: "access",
|
|
48
|
+
uuid: "00001800-0000-1000-8000-00805f9b34fb",
|
|
49
|
+
characteristics: [],
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export const Motherboard = {
|
|
2
|
+
name: "Motherboard",
|
|
3
|
+
companyId: 0x2a29,
|
|
4
|
+
services: [
|
|
5
|
+
{
|
|
6
|
+
name: "Device Information",
|
|
7
|
+
id: "device",
|
|
8
|
+
uuid: "0000180a-0000-1000-8000-00805f9b34fb",
|
|
9
|
+
characteristics: [
|
|
10
|
+
// {
|
|
11
|
+
// name: 'Serial Number (Blocked)',
|
|
12
|
+
// id: 'serial'
|
|
13
|
+
// uuid: '00002a25-0000-1000-8000-00805f9b34fb'
|
|
14
|
+
// },
|
|
15
|
+
{
|
|
16
|
+
name: "Firmware Revision",
|
|
17
|
+
id: "firmware",
|
|
18
|
+
uuid: "00002a26-0000-1000-8000-00805f9b34fb",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: "Hardware Revision",
|
|
22
|
+
id: "hardware",
|
|
23
|
+
uuid: "00002a27-0000-1000-8000-00805f9b34fb",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "Manufacturer Name",
|
|
27
|
+
id: "manufacturer",
|
|
28
|
+
uuid: "00002a29-0000-1000-8000-00805f9b34fb",
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "Battery Service",
|
|
34
|
+
id: "battery",
|
|
35
|
+
uuid: "0000180f-0000-1000-8000-00805f9b34fb",
|
|
36
|
+
characteristics: [
|
|
37
|
+
{
|
|
38
|
+
name: "Battery Level",
|
|
39
|
+
id: "level",
|
|
40
|
+
uuid: "00002a19-0000-1000-8000-00805f9b34fb",
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: "Unknown Service",
|
|
46
|
+
id: "unknown",
|
|
47
|
+
uuid: "10ababcd-15e1-28ff-de13-725bea03b127",
|
|
48
|
+
characteristics: [
|
|
49
|
+
{
|
|
50
|
+
name: "Unknown 01",
|
|
51
|
+
id: "01",
|
|
52
|
+
uuid: "10ab1524-15e1-28ff-de13-725bea03b127",
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: "Unknown 02",
|
|
56
|
+
id: "02",
|
|
57
|
+
uuid: "10ab1525-15e1-28ff-de13-725bea03b127",
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: "UART Nordic Service",
|
|
63
|
+
id: "uart",
|
|
64
|
+
uuid: "6e400001-b5a3-f393-e0a9-e50e24dcca9e",
|
|
65
|
+
characteristics: [
|
|
66
|
+
{
|
|
67
|
+
name: "TX",
|
|
68
|
+
id: "tx",
|
|
69
|
+
uuid: "6e400002-b5a3-f393-e0a9-e50e24dcca9e",
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: "RX",
|
|
73
|
+
id: "rx",
|
|
74
|
+
uuid: "6e400003-b5a3-f393-e0a9-e50e24dcca9e",
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Device } from "./types";
|
|
2
|
+
export declare const Tindeq: Device;
|
|
3
|
+
export declare const Commands: {
|
|
4
|
+
TARE_SCALE: number;
|
|
5
|
+
START_MEASURING: number;
|
|
6
|
+
STOP_MEASURING: number;
|
|
7
|
+
GET_APP_VERSION: number;
|
|
8
|
+
GET_ERROR_INFO: number;
|
|
9
|
+
CLEAR_ERR_INFO: number;
|
|
10
|
+
GET_BATTERY_LEVEL: number;
|
|
11
|
+
SLEEP: number;
|
|
12
|
+
};
|
|
13
|
+
export declare const NotificationTypes: {
|
|
14
|
+
COMMAND_RESPONSE: number;
|
|
15
|
+
WEIGHT_MEASURE: number;
|
|
16
|
+
LOW_BATTERY_WARNING: number;
|
|
17
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const Tindeq = {
|
|
2
|
+
name: "Tindeq",
|
|
3
|
+
services: [
|
|
4
|
+
{
|
|
5
|
+
name: "Progressor Service",
|
|
6
|
+
id: "progressor",
|
|
7
|
+
uuid: "7e4e1701-1ea6-40c9-9dcc-13d34ffead57",
|
|
8
|
+
characteristics: [
|
|
9
|
+
{
|
|
10
|
+
name: "Write",
|
|
11
|
+
id: "tx",
|
|
12
|
+
uuid: "7e4e1703-1ea6-40c9-9dcc-13d34ffead57",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: "Notify",
|
|
16
|
+
id: "rx",
|
|
17
|
+
uuid: "7e4e1702-1ea6-40c9-9dcc-13d34ffead57",
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
};
|
|
23
|
+
export const Commands = {
|
|
24
|
+
TARE_SCALE: 0x64,
|
|
25
|
+
START_MEASURING: 0x65,
|
|
26
|
+
STOP_MEASURING: 0x66,
|
|
27
|
+
GET_APP_VERSION: 0x6b,
|
|
28
|
+
GET_ERROR_INFO: 0x6c,
|
|
29
|
+
CLEAR_ERR_INFO: 0x6d,
|
|
30
|
+
GET_BATTERY_LEVEL: 0x6f,
|
|
31
|
+
SLEEP: 0x6e,
|
|
32
|
+
};
|
|
33
|
+
export const NotificationTypes = {
|
|
34
|
+
COMMAND_RESPONSE: 0,
|
|
35
|
+
WEIGHT_MEASURE: 1,
|
|
36
|
+
LOW_BATTERY_WARNING: 2,
|
|
37
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="web-bluetooth" />
|
|
2
|
+
interface Characteristic {
|
|
3
|
+
name: string;
|
|
4
|
+
id: string;
|
|
5
|
+
uuid: string;
|
|
6
|
+
characteristic?: BluetoothRemoteGATTCharacteristic;
|
|
7
|
+
}
|
|
8
|
+
interface Service {
|
|
9
|
+
name: string;
|
|
10
|
+
id: string;
|
|
11
|
+
uuid: string;
|
|
12
|
+
characteristics: Characteristic[];
|
|
13
|
+
}
|
|
14
|
+
export interface Device {
|
|
15
|
+
name: string;
|
|
16
|
+
companyId?: number;
|
|
17
|
+
services: Service[];
|
|
18
|
+
device?: BluetoothDevice;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
package/src/notify.d.ts
ADDED
package/src/notify.js
ADDED
package/src/read.d.ts
ADDED
package/src/read.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { notifyCallback } from "./notify";
|
|
2
|
+
import { getCharacteristic } from "./characteristic";
|
|
3
|
+
/**
|
|
4
|
+
* read
|
|
5
|
+
* @param characteristic
|
|
6
|
+
*/
|
|
7
|
+
export const read = (board, serviceId, characteristicId, duration = 0) => {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
if (board.device?.gatt?.connected) {
|
|
10
|
+
const characteristic = getCharacteristic(board, serviceId, characteristicId);
|
|
11
|
+
if (characteristic) {
|
|
12
|
+
characteristic
|
|
13
|
+
.readValue()
|
|
14
|
+
.then((value) => {
|
|
15
|
+
let decodedValue;
|
|
16
|
+
const decoder = new TextDecoder("utf-8");
|
|
17
|
+
switch (characteristicId) {
|
|
18
|
+
case "level":
|
|
19
|
+
decodedValue = value.getUint8(0);
|
|
20
|
+
break;
|
|
21
|
+
default:
|
|
22
|
+
decodedValue = decoder.decode(value);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
if (notifyCallback) {
|
|
26
|
+
notifyCallback({ uuid: characteristic.uuid, value: decodedValue });
|
|
27
|
+
}
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
resolve();
|
|
30
|
+
}, duration);
|
|
31
|
+
})
|
|
32
|
+
.catch((error) => {
|
|
33
|
+
reject(error);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
reject(new Error("Characteristic is undefined"));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
reject(new Error("Device is not connected"));
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
};
|
package/src/read.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { getCharacteristic } from "./characteristic"
|
|
|
6
6
|
* read
|
|
7
7
|
* @param characteristic
|
|
8
8
|
*/
|
|
9
|
-
export const read = (board: Device, serviceId: string, characteristicId: string): Promise<void> => {
|
|
9
|
+
export const read = (board: Device, serviceId: string, characteristicId: string, duration: number = 0): Promise<void> => {
|
|
10
10
|
return new Promise((resolve, reject) => {
|
|
11
11
|
if (board.device?.gatt?.connected) {
|
|
12
12
|
const characteristic = getCharacteristic(board, serviceId, characteristicId)
|
|
@@ -25,8 +25,12 @@ export const read = (board: Device, serviceId: string, characteristicId: string)
|
|
|
25
25
|
decodedValue = decoder.decode(value)
|
|
26
26
|
break
|
|
27
27
|
}
|
|
28
|
-
notifyCallback
|
|
29
|
-
|
|
28
|
+
if (notifyCallback) {
|
|
29
|
+
notifyCallback({ uuid: characteristic.uuid, value: decodedValue })
|
|
30
|
+
}
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
resolve()
|
|
33
|
+
}, duration)
|
|
30
34
|
})
|
|
31
35
|
.catch((error) => {
|
|
32
36
|
reject(error)
|
package/src/write.d.ts
ADDED
package/src/write.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getCharacteristic } from "./characteristic";
|
|
2
|
+
/**
|
|
3
|
+
* write
|
|
4
|
+
* @param characteristic
|
|
5
|
+
* @param message
|
|
6
|
+
*/
|
|
7
|
+
export const write = (board, serviceId, characteristicId, message, duration = 0) => {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
if (board.device?.gatt?.connected) {
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
const characteristic = getCharacteristic(board, serviceId, characteristicId);
|
|
12
|
+
if (characteristic) {
|
|
13
|
+
characteristic
|
|
14
|
+
.writeValue(encoder.encode(message))
|
|
15
|
+
.then(() => {
|
|
16
|
+
setTimeout(() => {
|
|
17
|
+
resolve();
|
|
18
|
+
}, duration);
|
|
19
|
+
})
|
|
20
|
+
.catch((error) => {
|
|
21
|
+
reject(error);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
reject(new Error("Characteristics is undefined"));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
reject(new Error("Device is not connected"));
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|