-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-noble.js
49 lines (39 loc) · 1.43 KB
/
example-noble.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
import noble from '@abandonware/noble'
import {TileServiceNoble, TileVolume, UserService} from 'node-tile'
// We have to log into Tile to get the authkeys of your tile
const userService = new UserService("<your-tile-email>", "<your-tile-password>")
await userService.login()
const tiles = await userService.getTiles()
console.log(tiles)
noble.on('stateChange', state => {
if (state === 'poweredOn'){
noble.startScanning([], false);
}else{
console.log(`Couldn't start scanning, ${state}`)
noble.stopScanning();
}
});
noble.on('discover', async peripheral => {
let address = peripheral.address.split(":").join("")
// If we find ANY tile in our account, we continue
let tile = tiles.filter(t => t.id.startsWith(address))[0]
if(!tile){
console.log("Non-tile", address)
return
}
console.log("TILE", peripheral.address, peripheral.rssi, tile)
noble.stopScanning()
// Create the service based on bluetooth device & tile from our account
let service = new TileServiceNoble(peripheral, tile)
service.on("singleTab", _ => console.log("Got single tab!"))
service.on("doubleTab", _ => console.log("Got double tab!"))
service.on("rssi", rssi => console.log("rssi", rssi))
service.on("tileRssi", rssi => console.log("tileRssi", rssi))
service.on("debug", msg => console.log("debug", msg))
await service.connect()
await service.sendRinger(TileVolume.MED)
});
process.on('SIGINT', _ => {
noble.stopScanning();
process.exit();
})