-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·105 lines (88 loc) · 2.99 KB
/
index.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
#!/usr/bin/env node
var noble = require('noble-mac');
var argv = require('minimist')(process.argv.slice(2));
//Must provide the target device name as an argument
if (argv._.length != 1) {
printHelp();
process.exit(0);
}
var deviceName = argv._[0].toLowerCase();
//Optionally provide the service UUID with -serviceUUID "xxxx...xxxx"
var serviceUUID = (argv.serviceUUID && argv.serviceUUID.split(',')) || ["6e400001b5a3f393e0a9e50e24dcca9e"];
//Optionally provide the rx/tx UUIDs with comma-separated -characteristicUUID "xxxx...xxxx","xxxx...xxxx"
var characteristicUUIDs = (argv.charUUIDs && argv.charUUIDs.split(',')) || [];
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
console.log(`Scanning for ${deviceName}... (Ctrl-D to stop)`);
noble.startScanning();
} else {
console.log('Scanning stopped - is Bluetooth adapter connected / turned on?');
noble.stopScanning();
}
});
noble.on('discover', function(peripheral) {
var advertisement = peripheral.advertisement;
var localName = advertisement.localName;
if (localName && localName.toLowerCase() == deviceName) {
noble.stopScanning();
console.log(`Connected to ${deviceName}!`);
console.log(`(Ctrl-D to disconnect)`);
console.log();
connectToUart(peripheral);
}
});
var uartReadChar = null;
var uartWriteChar = null;
function connectToUart(peripheral) {
peripheral.once('disconnect', function() {
console.log();
console.log(`Disconnected. Scanning for ${deviceName}...`);
uartReadChar = null;
uartWriteChar = null;
noble.startScanning();
});
peripheral.connect(function(err) {
peripheral.discoverSomeServicesAndCharacteristics(
serviceUUID,
characteristicUUIDs,
function(err, services, characteristics) {
for(i=0; i<characteristics.length; i++) {
var char = characteristics[i];
if (char.properties.includes('notify')) {
uartReadChar = char;
uartReadChar.subscribe(function(err) {
if (err) {
console.error(`Failed to subscribe to read characteristic: ${err}`);
}
});
uartReadChar.on('data', function(data) {
process.stdout.write(data);
});
} else if (char.properties.includes('write')) {
uartWriteChar = char;
}
}
}
);
});
}
if (process.stdin.isTTY && process.stdout.isTTY) {
process.stdin.setRawMode(true);
process.stdin.on('data', function(data) {
if (data) {
if ((data.length === 1) && (data[0] === 0x04)) { //Ctrl-D
process.exit(0);
}
if (uartWriteChar) {
uartWriteChar.write(data, false, function(err) {
if (err) {
console.error(`Failed to write data to write characteristic: ${err}`);
}
});
}
}
});
}
function printHelp() {
console.log('Usage: bleterm2 [-serviceUUID "xxxx..."] [-charUUIDs "xxxx..","xxxx.."] <device name>');
}