-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
182 lines (145 loc) · 5.92 KB
/
index.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
import { NativeModules, NativeEventEmitter } from 'react-native';
import EventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter';
const blePeripheralModule = NativeModules.P2PTransferBLEPeripheralModule;
const blePeripheralEmitter = new NativeEventEmitter(blePeripheralModule);
class BLEPeripheral extends EventEmitter {
constructor() {
super();
}
isSupported = () => {
return new Promise((resolve, reject) => {
blePeripheralModule.isSupported(isSupported => {
resolve(isSupported);
});
});
}
publish = (serviceUUID, characteristicUUID, localName) => {
return new Promise((resolve, reject) => {
this.isSupported().then((supported) => {
if(!supported) {
return reject("BLE is not supported on your device.");
}
blePeripheralModule.start((started) => {
if(!started) {
return reject("Unable to start. Make sure bluetooth is enabled.");
}
this.unpublish().then(() => {
console.log("unpublish...");
blePeripheralModule.addService(serviceUUID, (data) => {
this.addedServiceUUID = serviceUUID;
console.log("addService...", data);
blePeripheralModule.addCharacteristic(serviceUUID, characteristicUUID, (data) => {
this.addedCharacteristicUUID = characteristicUUID;
console.log("addCharacteristic...", data);
blePeripheralModule.publishService(serviceUUID, (data) => {
console.log("publishedService...", data);
blePeripheralModule.startAdvertising(localName, (data) => {
console.log("startAdvertising...", data);
resolve(data);
});
});
});
});
});
});
});
});
}
stopAdvertising = () => {
return new Promise((resolve, reject) => {
blePeripheralModule.stopAdvertising(() => {
console.log('stopped advertising...');
resolve();
});
});
}
unpublish = () => {
return new Promise((resolve, reject) => {
this.stopAdvertising()
.then(() => {
console.log("unpublishing...");
if(this.addedServiceUUID === undefined) {
console.log('already unpublished');
return resolve();
}
let tempUUID = this.addedServiceUUID;
this.addedServiceUUID = undefined;
this.addedCharacteristicUUID = undefined;
blePeripheralModule.unpublishService(tempUUID, () => {
console.log('unpublished');
return resolve();
});
});
});
}
sendData = (value, filter, sendCharacteristicUUID) => {
return new Promise((resolve, reject) => {
const {serviceUUID, localName} = filter;
const characteristicUUID = sendCharacteristicUUID ? sendCharacteristicUUID : '2222'; // Special characteristic for sending data. Central subscribes to this.
if(!serviceUUID) {
return reject("serviceUUID required filter");
}
blePeripheralModule.setSendCharacteristic(characteristicUUID);
blePeripheralEmitter.removeAllListeners('didSubscribeToCharacteristic');
blePeripheralEmitter.addListener('didSubscribeToCharacteristic', (subscriber) => {
console.log('didSubscribeToCharacteristic', subscriber, serviceUUID, characteristicUUID);
if(subscriber.serviceUUID.toLowerCase() !== serviceUUID.toLowerCase() ||
subscriber.characteristicUUID.toLowerCase() !== characteristicUUID.toLowerCase()) {
return ;
}
blePeripheralEmitter.removeAllListeners('sendingProgress');
blePeripheralEmitter.removeAllListeners('didSubscribeToCharacteristic');
console.log('updateValue...');
blePeripheralEmitter.addListener('sendingProgress', (data) => {
this.emit('sendingProgress', data);
});
this.emit('sendingStarted');
blePeripheralModule.updateValue(value, subscriber.centralUUID, serviceUUID, characteristicUUID, () => {
setTimeout(() => {
this.unpublish()
.then(() => {
this.emit('sendingDone');
return resolve();
});
}, 1000)
});
});
this.publish(serviceUUID, characteristicUUID, localName)
.then(() => {})
.catch((err) => { reject(err); });
});
}
receiveData = (filter, receiveCharacteristicUUID) => {
return new Promise((resolve, reject) => {
const {serviceUUID, localName} = filter;
const characteristicUUID = receiveCharacteristicUUID ? receiveCharacteristicUUID : '3333'; // Special characteristic for receiving data. Central starts sending once Central subscribes.
if(!serviceUUID) {
return reject("serviceUUID required filter");
}
blePeripheralModule.setReceiveCharacteristic(characteristicUUID);
console.log('add listeners');
blePeripheralEmitter.removeAllListeners('transferStarted');
blePeripheralEmitter.removeAllListeners('transferProgress');
blePeripheralEmitter.removeAllListeners('transferDone');
blePeripheralEmitter.addListener('transferStarted', (data) => {
blePeripheralEmitter.removeAllListeners('transferStarted');
this.emit('receivingStarted', data);
});
blePeripheralEmitter.addListener('transferProgress', (data) => {
this.emit('receivingProgress', data);
});
blePeripheralEmitter.addListener('transferDone', (data) => {
blePeripheralEmitter.removeAllListeners('transferDone');
blePeripheralEmitter.removeAllListeners('transferProgress');
this.unpublish().then(() => {
this.emit('receivingDone', data);
return resolve(data);
});
});
this.publish(serviceUUID, characteristicUUID, localName)
.then(() => {})
.catch((err) => { reject(err); });
});
}
}
module.exports = new BLEPeripheral();