-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathDevice.js
More file actions
189 lines (166 loc) · 4.36 KB
/
Copy pathDevice.js
File metadata and controls
189 lines (166 loc) · 4.36 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
189
const EventEmitter = require('events')
const BusHelper = require('./BusHelper')
const GattServer = require('./GattServer')
/**
* @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')
}
/**
* 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')
}
/**
* Service advertisement data. Keys are the UUIDs in string format followed by its byte array value.
* @returns {object}
*/
async getServiceAdvertisementData () {
return this.helper.prop('ServiceData')
}
/**
* Manufacturer specific advertisement data. Keys are 16 bits Manufacturer ID followed by its byte array value.
* @returns {object}
*/
async getManufacturerAdvertisementData () {
return this.helper.prop('ManufacturerData')
}
/**
* List of 128-bit UUIDs that represents the available remote services.
* @returns {string[]}
*/
async getServiceUUIDs () {
return this.helper.prop('UUIDs')
}
/**
* 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, deviceUuid: this.device })
} else {
this.emit('disconnect', { connected: false, deviceUuid: this.device })
this.helper.removeAllListeners('PropertiesChanged') // might be improved
}
}
}
this.helper.on('PropertiesChanged', cb)
await this.helper.callMethod('Connect')
}
/**
* Disconnect remote device
*/
async disconnect () {
await this.helper.callMethod('Disconnect')
}
/**
* 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.
* @property {string} deviceUuid - The UUID of the device freshly connected
*/
/**
* Disconection event
*
* @event Device#disconnect
* @type {object}
* @property {boolean} connected - Indicates current connection status.
* @property {string} deviceUuid - The UUID of the device freshly disconnected
*/
module.exports = Device