forked from jacobrosenthal/react-native-ble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindings.ios.js
233 lines (177 loc) · 8.57 KB
/
bindings.ios.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var debug = require('debug')('ios-bindings');
var util = require('util');
var events = require('events');
var Buffer = require('buffer').Buffer;
/**
* NobleBindings for react native
*/
var NobleBindings = function() {
this.RNBLE = null;
};
util.inherits(NobleBindings, events.EventEmitter);
NobleBindings.prototype.onConnect = function(message) {
this.emit('connect', message.peripheralUuid, message.error);
};
NobleBindings.prototype.onDisconnect = function(message) {
this.emit('disconnect', message.peripheralUuid);
};
NobleBindings.prototype.onRssiUpdate = function(message) {
this.emit('rssiUpdate', message.peripheralUuid, message.rssi);
};
NobleBindings.prototype.onServicesDiscover = function(message) {
this.emit('servicesDiscover', message.peripheralUuid, message.serviceUuids);
};
NobleBindings.prototype.onIncludedServicesDiscover = function(message) {
this.emit('includedServicesDiscover', message.peripheralUuid, message.serviceUuid, message.includedServiceUuids);
};
NobleBindings.prototype.onCharacteristicsDiscover = function(message) {
this.emit('characteristicsDiscover', message.peripheralUuid, message.serviceUuid, message.characteristics);
};
NobleBindings.prototype.onDescriptorsDiscover = function(message) {
this.emit('descriptorsDiscover', message.peripheralUuid, message.serviceUuid, message.characteristicUuid, message.descriptors);
};
NobleBindings.prototype.onData = function(message) {
var processedData = new Buffer(message.data, 'base64');
this.emit('data', message.peripheralUuid, message.serviceUuid, message.characteristicUuid, processedData, message.isNotification);
this.emit('read', message.peripheralUuid, message.serviceUuid, message.characteristicUuid, processedData, message.isNotification);
};
NobleBindings.prototype.onWrite = function(message) {
this.emit('write', message.peripheralUuid, message.serviceUuid, message.characteristicUuid);
};
NobleBindings.prototype.onValueWrite = function(message) {
var processedData = new Buffer(message.data, 'base64');
this.emit('valueWrite', message.peripheralUuid, message.serviceUuid, message.characteristicUuid, message.descriptorUuid);
};
NobleBindings.prototype.onValueUpdate = function(message) {
this.emit('valueRead', message.peripheralUuid, message.serviceUuid, message.characteristicUuid, message.descriptorUuid, message.data);
};
NobleBindings.prototype.onNotify = function(message) {
this.emit('notify', message.peripheralUuid, message.serviceUuid, message.characteristicUuid, message.state);
};
NobleBindings.prototype.onDiscover = function(message) {
debug('peripheral ' + message.peripheralUuid + ' discovered');
if (message.advertisement.manufacturerData) {
message.advertisement.manufacturerData = new Buffer(message.advertisement.manufacturerData, 'base64');
}
if (message.advertisement.serviceData) {
message.advertisement.serviceData = message.advertisement.serviceData.map((ad) => ({
uuid: ad.uuid,
data: new Buffer(ad.data, 'base64'),
}));
}
// We don't know these values because iOS doesn't want to give us
// this information. Only random UUIDs are generated from them
// under the hood
var address = 'unknown';
var addressType = 'unknown';
this.emit('discover', message.peripheralUuid, address, addressType, message.connectable, message.advertisement, message.rssi);
};
NobleBindings.prototype.onStateChange = function(state) {
// var state = ['unknown', 'resetting', 'unsupported', 'unauthorized', 'poweredOff', 'poweredOn'][args.kCBMsgArgState];
debug('state change ' + state);
this.emit('stateChange', state);
};
var nobleBindings = new NobleBindings();
/**
* Start scanning
* @param {Array} serviceUuids Scan for these UUIDs, if undefined then scan for all
* @param {Bool} allowDuplicates Scan can return duplicates
*
* @discussion tested
*/
nobleBindings.startScanning = function(serviceUuids, allowDuplicates) {
var duplicates = allowDuplicates || false;
this.RNBLE.startScanning(toAppleUuids(serviceUuids), duplicates);
this.emit('scanStart');
};
/**
* Stop scanning
*
* @discussion tested
*/
nobleBindings.stopScanning = function() {
this.RNBLE.stopScanning();
this.emit('scanStop');
};
nobleBindings.init = function(native) {
if(native) {
this.RNBLE = native.RNBLE;
this.DeviceEventEmitter = native.DeviceEventEmitter;
}else {
this.RNBLE = require('react-native').NativeModules.RNBLE;
this.DeviceEventEmitter = require('react-native').DeviceEventEmitter;
}
this.DeviceEventEmitter.addListener('ble.connect', this.onConnect.bind(this));
this.DeviceEventEmitter.addListener('ble.disconnect', this.onDisconnect.bind(this));
this.DeviceEventEmitter.addListener('ble.discover', this.onDiscover.bind(this));
this.DeviceEventEmitter.addListener('ble.rssiUpdate', this.onRssiUpdate.bind(this));
this.DeviceEventEmitter.addListener('ble.servicesDiscover', this.onServicesDiscover.bind(this));
this.DeviceEventEmitter.addListener('ble.includedServicesDiscover', this.onIncludedServicesDiscover.bind(this));
this.DeviceEventEmitter.addListener('ble.characteristicsDiscover', this.onCharacteristicsDiscover.bind(this));
this.DeviceEventEmitter.addListener('ble.descriptorsDiscover', this.onDescriptorsDiscover.bind(this));
this.DeviceEventEmitter.addListener('ble.stateChange', this.onStateChange.bind(this));
this.DeviceEventEmitter.addListener('ble.data', this.onData.bind(this));
this.DeviceEventEmitter.addListener('ble.write', this.onWrite.bind(this));
this.DeviceEventEmitter.addListener('ble.notify', this.onNotify.bind(this));
this.DeviceEventEmitter.addListener('ble.valueUpdate', this.onValueUpdate.bind(this));
this.DeviceEventEmitter.addListener('ble.valueWrite', this.onValueWrite.bind(this));
setTimeout(function() {
this.RNBLE.getState();
}.bind(this), 1000);
};
nobleBindings.connect = function(deviceUuid) {
this.RNBLE.connect(toAppleUuid(deviceUuid));
};
nobleBindings.disconnect = function(deviceUuid) {
this.RNBLE.disconnect(toAppleUuid(deviceUuid));
};
nobleBindings.updateRssi = function(deviceUuid) {
this.RNBLE.updateRssi(toAppleUuid(deviceUuid));
};
nobleBindings.discoverServices = function(deviceUuid, uuids) {
this.RNBLE.discoverServices(toAppleUuid(deviceUuid), toAppleUuids(uuids));
};
nobleBindings.discoverIncludedServices = function(deviceUuid, serviceUuid, serviceUuids) {
this.RNBLE.discoverIncludedServices(toAppleUuid(deviceUuid), toAppleUuid(serviceUuid), toAppleUuids(serviceUuids));
};
nobleBindings.discoverCharacteristics = function(deviceUuid, serviceUuid, characteristicUuids) {
this.RNBLE.discoverCharacteristics(toAppleUuid(deviceUuid), toAppleUuid(serviceUuid), toAppleUuids(characteristicUuids));
};
nobleBindings.read = function(deviceUuid, serviceUuid, characteristicUuid) {
this.RNBLE.read(toAppleUuid(deviceUuid), toAppleUuid(serviceUuid), toAppleUuid(characteristicUuid));
};
nobleBindings.write = function(deviceUuid, serviceUuid, characteristicUuid, data, withoutResponse) {
this.RNBLE.write(toAppleUuid(deviceUuid), toAppleUuid(serviceUuid), toAppleUuid(characteristicUuid), data.toString('base64'), withoutResponse);
};
nobleBindings.notify = function(deviceUuid, serviceUuid, characteristicUuid, notify) {
this.RNBLE.notify(toAppleUuid(deviceUuid), toAppleUuid(serviceUuid), toAppleUuid(characteristicUuid), notify);
};
nobleBindings.discoverDescriptors = function(deviceUuid, serviceUuid, characteristicUuid) {
this.RNBLE.discoverDescriptors(toAppleUuid(deviceUuid), toAppleUuid(serviceUuid), toAppleUuid(characteristicUuid));
};
nobleBindings.readValue = function(deviceUuid, serviceUuid, characteristicUuid, descriptorUuid) {
this.RNBLE.readValue(toAppleUuid(deviceUuid), toAppleUuid(serviceUuid), toAppleUuid(characteristicUuid), toAppleUuid(descriptorUuid));
};
nobleBindings.writeValue = function(deviceUuid, serviceUuid, characteristicUuid, descriptorUuid, data) {
this.RNBLE.writeValue(toAppleUuid(deviceUuid), toAppleUuid(serviceUuid), toAppleUuid(characteristicUuid), toAppleUuid(descriptorUuid), data.toString('base64'), withoutResponse);
};
nobleBindings.readHandle = function(deviceUuid, handle) {
throw new Error('readHandle not implemented on ios');
};
nobleBindings.writeHandle = function(deviceUuid, handle, data, withoutResponse) {
throw new Error('writeHandle not implemented on ios');
};
function toAppleUuid(uuid) {
return uuid.replace(/(\S{8})(\S{4})(\S{4})(\S{4})(\S{12})/, "$1-$2-$3-$4-$5").toUpperCase();
}
function toAppleUuids(uuids) {
var convertedUuids = [];
if (uuids) {
uuids.forEach(function(uuid) {
convertedUuids.push(toAppleUuid(uuid));
});
}
return convertedUuids;
}
// Exports
module.exports = nobleBindings;