-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathDevice.js
More file actions
188 lines (165 loc) · 4.12 KB
/
Copy pathDevice.js
File metadata and controls
188 lines (165 loc) · 4.12 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const EventEmitter = require('events')
const BusHelper = require('./BusHelper')
const GattServer = require('./GattServer')
const parseDict = require('./parseDict')
/**
* @classdesc Device class interacts with a remote device.
* @class Device
* @extends EventEmitter
* @see You can construct a Device object via {@link Adapter#getDevice} method
*/
class Device extends EventEmitter {
constructor (dbus, adapter, device) {
super()
this.dbus = dbus
this.adapter = adapter
this.device = device
this.helper = new BusHelper(dbus, 'org.bluez', `/org/bluez/${adapter}/${device}`, 'org.bluez.Device1', { usePropsEvents: true })
}
/**
* The Bluetooth remote name.
* @returns {string}
*/
async getName () {
return this.helper.prop('Name')
}
/**
* The Bluetooth device address of the remote device.
* @returns {string}
*/
async getAddress () {
return this.helper.prop('Address')
}
/**
* The Bluetooth device Address Type (public, random).
* @returns {string}
*/
async getAddressType () {
return this.helper.prop('AddressType')
}
/**
* The name alias for the remote device.
* @returns {string}
*/
async getAlias () {
return this.helper.prop('Alias')
}
/**
* Received Signal Strength Indicator of the remote device
* @returns {number}
*/
async getRSSI () {
return this.helper.prop('RSSI')
}
/**
* Advertised transmitted power level.
* @returns {number}
*/
async getTXPower () {
return this.helper.prop('TxPower')
}
/**
* Advertised transmitted manufacturer data.
* @returns {Object.<string, any>}
*/
async getManufacturerData () {
return parseDict(await this.helper.prop('ManufacturerData'))
}
/**
* Advertised transmitted data. (experimental: this feature might not be fully supported by bluez)
* @returns {Object.<string, any>}
*/
async getAdvertisingData () {
return parseDict(await this.helper.prop('AdvertisingData'))
}
/**
* Advertised transmitted data.
* @returns {Object.<string, any>}
*/
async getServiceData () {
return parseDict(await this.helper.prop('ServiceData'))
}
/**
* Indicates if the remote device is paired.
* @returns {boolean}
*/
async isPaired () {
return this.helper.prop('Paired')
}
/**
* Indicates if the remote device is currently connected.
* @returns {boolean}
*/
async isConnected () {
return this.helper.prop('Connected')
}
/**
* This method will connect to the remote device
*/
async pair () {
return this.helper.callMethod('Pair')
}
/**
* This method can be used to cancel a pairing operation initiated by the Pair method.
*/
async cancelPair () {
return this.helper.callMethod('CancelPair')
}
/**
* Connect to remote device
*/
async connect () {
const cb = (propertiesChanged) => {
if ('Connected' in propertiesChanged) {
const { value } = propertiesChanged.Connected
if (value) {
this.emit('connect', { connected: true })
} else {
this.emit('disconnect', { connected: false })
}
}
}
this.helper.on('PropertiesChanged', cb)
await this.helper.callMethod('Connect')
}
/**
* Disconnect remote device
*/
async disconnect () {
await this.helper.callMethod('Disconnect')
this.helper.removeAllListeners()
}
/**
* Init a GattServer instance and return it
* @returns {GattServer}
*/
async gatt () {
const gattServer = new GattServer(this.dbus, this.adapter, this.device)
await gattServer.init()
return gattServer
}
/**
* Human readable class identifier.
* @returns {string}
*/
async toString () {
const name = await this.getName()
const address = await this.getAddress()
return `${name} [${address}]`
}
}
/**
* Connection event
*
* @event Device#connect
* @type {object}
* @property {boolean} connected - Indicates current connection status.
*/
/**
* Disconection event
*
* @event Device#disconnect
* @type {object}
* @property {boolean} connected - Indicates current connection status.
*/
module.exports = Device