-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
64 lines (50 loc) · 1.75 KB
/
example.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
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("<email>", "<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("")
// Create the service based on bluetooth device
let bleTile = new TileServiceNoble(peripheral)
bleTile.on("debug", msg => console.log("debug", msg))
if(!bleTile.isTile()){
console.log("Non-tile", address, peripheral.advertisement)
return
}
if(!bleTile.isTileActivated()) {
console.log("Non-activated tile, please activate in app first", address)
return
}
console.log("TILE FOUND", peripheral.address);
// To be able to get the tileId, we have to connect to the tile
noble.stopScanning()
await bleTile.connect()
// Based on the tileId, we can find the tile in our account
let tileId = await bleTile.getTileId()
let accTile = tiles.filter(t => t.id.startsWith(tileId))[0]
if(!accTile){
console.log("Tile not found in account", tileId)
return
}
await bleTile.authenticate(accTile)
bleTile.on("singleTab", _ => console.log("Got single tab!"))
bleTile.on("doubleTab", _ => console.log("Got double tab!"))
bleTile.on("rssi", rssi => console.log("rssi", rssi))
bleTile.on("tileRssi", rssi => console.log("tileRssi", rssi))
await bleTile.sendRinger(TileVolume.MED)
});
process.on('SIGINT', _ => {
noble.stopScanning();
process.exit();
})