Skip to content

Commit 4a274ae

Browse files
committed
chore: ble server test for linux
1 parent 10bb8ec commit 4a274ae

File tree

4 files changed

+125
-5
lines changed

4 files changed

+125
-5
lines changed

ble_server_test/index.linux.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const HciSocket = require('hci-socket');
2+
const NodeBleHost = require('ble-host');
3+
const BleManager = NodeBleHost.BleManager;
4+
const AdvertisingDataBuilder = NodeBleHost.AdvertisingDataBuilder;
5+
const HciErrors = NodeBleHost.HciErrors;
6+
const AttErrors = NodeBleHost.AttErrors;
7+
8+
const deviceName = 'MyDevice';
9+
10+
const transport = new HciSocket(); // connects to the first hci device on the computer, for example hci0
11+
12+
const options = {
13+
// optional properties go here
14+
};
15+
16+
BleManager.create(transport, options, function (err, manager) {
17+
// err is either null or an Error object
18+
// if err is null, manager contains a fully initialized BleManager object
19+
if (err) {
20+
console.error(err);
21+
return;
22+
}
23+
24+
let notificationCharacteristic;
25+
26+
manager.gattDb.setDeviceName(deviceName);
27+
manager.gattDb.addServices([
28+
{
29+
uuid: '22222222-3333-4444-5555-666666666666',
30+
characteristics: [
31+
{
32+
uuid: '22222222-3333-4444-5555-666666666667',
33+
properties: ['read', 'write'],
34+
value: 'some default value' // could be a Buffer for a binary value
35+
},
36+
{
37+
uuid: '22222222-3333-4444-5555-666666666668',
38+
properties: ['read'],
39+
onRead(connection, callback) {
40+
callback(AttErrors.SUCCESS, new Date().toString());
41+
}
42+
},
43+
{
44+
uuid: '22222222-3333-4444-5555-666666666669',
45+
properties: ['write'],
46+
onWrite(connection, needsResponse, value, callback) {
47+
console.log('A new value was written:', value);
48+
callback(AttErrors.SUCCESS); // actually only needs to be called when needsResponse is true
49+
}
50+
},
51+
(notificationCharacteristic = {
52+
uuid: '22222222-3333-4444-5555-66666666666A',
53+
properties: ['notify'],
54+
onSubscriptionChange(connection, notification, indication, isWrite) {
55+
if (notification) {
56+
// Notifications are now enabled, so let's send something
57+
notificationCharacteristic.notify(connection, 'Sample notification');
58+
}
59+
}
60+
})
61+
]
62+
}
63+
]);
64+
65+
const advDataBuffer = new AdvertisingDataBuilder()
66+
.addFlags(['leGeneralDiscoverableMode', 'brEdrNotSupported'])
67+
.addLocalName(/*isComplete*/ true, deviceName)
68+
.add128BitServiceUUIDs(/*isComplete*/ true, ['22222222-3333-4444-5555-666666666666'])
69+
.build();
70+
manager.setAdvertisingData(advDataBuffer);
71+
// call manager.setScanResponseData(...) if scan response data is desired too
72+
startAdv();
73+
74+
function startAdv() {
75+
manager.startAdvertising(
76+
{
77+
/*options*/
78+
},
79+
connectCallback
80+
);
81+
}
82+
83+
function connectCallback(status, conn) {
84+
if (status !== HciErrors.SUCCESS) {
85+
// Advertising could not be started for some controller-specific reason, try again after 10 seconds
86+
setTimeout(startAdv, 10000);
87+
return;
88+
}
89+
conn.on('disconnect', startAdv); // restart advertising after disconnect
90+
console.log('Connection established!', conn);
91+
}
92+
});

ble_server_test/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ bleno.on('stateChange', function (state) {
193193
// if (bleno.startAdvertisingWithEIRData) {
194194
// bleno.startAdvertisingWithEIRData(advertisement[0], advertisement[1]);
195195
// } else {
196-
bleno.startAdvertising(name);
196+
bleno.startAdvertising(name, ['ec00']);
197197
// }
198198
} else {
199199
bleno.stopAdvertising();

ble_server_test/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"start": "BLENO_DEVICE_NAME=echo ts-node index.ts"
7+
"start": "BLENO_DEVICE_NAME=echo ts-node index.ts",
8+
"start-linux": "BLENO_DEVICE_NAME=echo ts-node index.linux.ts"
89
},
910
"dependencies": {
10-
"@abandonware/bleno": "0.5.1-4"
11+
"@abandonware/bleno": "0.5.1-4",
12+
"ble-host": "^1.0.3",
13+
"hci-socket": "^1.0.0"
1114
},
1215
"author": "",
1316
"license": "ISC",

ble_server_test/yarn.lock

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ __metadata:
363363
languageName: node
364364
linkType: hard
365365

366-
"bindings@npm:^1.5.0":
366+
"bindings@npm:^1.5.0, bindings@npm:~1.5.0":
367367
version: 1.5.0
368368
resolution: "bindings@npm:1.5.0"
369369
dependencies:
@@ -372,13 +372,27 @@ __metadata:
372372
languageName: node
373373
linkType: hard
374374

375+
"ble-host@npm:^1.0.3":
376+
version: 1.0.3
377+
resolution: "ble-host@npm:1.0.3"
378+
dependencies:
379+
hci-socket: ^1.0.0
380+
dependenciesMeta:
381+
hci-socket:
382+
optional: true
383+
checksum: b8d4d1ebe4e68485458ad05b57fb6023754375a817b00aface3d8aa6d62a9483894491b695673a92c8e91f1fba34201699125ba320e8aea45d89798cf8e5c99b
384+
languageName: node
385+
linkType: hard
386+
375387
"ble_server_test@workspace:.":
376388
version: 0.0.0-use.local
377389
resolution: "ble_server_test@workspace:."
378390
dependencies:
379391
"@abandonware/bleno": 0.5.1-4
380392
"@types/bleno": ^0.4.2
381393
"@types/node": ^18.11.18
394+
ble-host: ^1.0.3
395+
hci-socket: ^1.0.0
382396
ts-node: ^10.9.1
383397
typescript: ^4.9.4
384398
languageName: unknown
@@ -828,6 +842,17 @@ __metadata:
828842
languageName: node
829843
linkType: hard
830844

845+
"hci-socket@npm:^1.0.0":
846+
version: 1.0.0
847+
resolution: "hci-socket@npm:1.0.0"
848+
dependencies:
849+
bindings: ~1.5.0
850+
node-addon-api: ^3.0.0
851+
node-gyp: latest
852+
conditions: os=linux
853+
languageName: node
854+
linkType: hard
855+
831856
"http-cache-semantics@npm:^4.1.0":
832857
version: 4.1.0
833858
resolution: "http-cache-semantics@npm:4.1.0"
@@ -1256,7 +1281,7 @@ __metadata:
12561281
languageName: node
12571282
linkType: hard
12581283

1259-
"node-addon-api@npm:^3.1.0":
1284+
"node-addon-api@npm:^3.0.0, node-addon-api@npm:^3.1.0":
12601285
version: 3.2.1
12611286
resolution: "node-addon-api@npm:3.2.1"
12621287
dependencies:

0 commit comments

Comments
 (0)