-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
137 lines (123 loc) · 3.55 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
'use strict';
import React from 'react-native';
import EventEmitter from 'events';
import _ from 'lodash';
import { Buffer } from 'buffer';
const { NativeAppEventEmitter, NativeModules } = React;
const { BluetoothModule } = NativeModules;
const PERIPHERAL_POLL_MS = 5000;
const CHANGE_EVENT = "change";
const g_eventEmitter = new EventEmitter();
let g_isPoweredOn = false;
let g_isReady = false;
let g_connectedPeripheralCount = 0;
NativeAppEventEmitter.addListener('bluetooth.peripheral.write',_onPeripheralWrite);
NativeAppEventEmitter.addListener('bluetooth.peripheral.read',_onPeripheralRead);
NativeAppEventEmitter.addListener('bluetooth.peripheral.update_value',_onPeripheralUpdateValue);
NativeAppEventEmitter.addListener('bluetooth.central.state',_onCentralState);
function init(opts) {
BluetoothModule.init(opts,(err) => {
BluetoothModule.getState((err,state) => {
_onCentralState(state,'startup');
});
setInterval(_checkPeripherials,PERIPHERAL_POLL_MS);
});
}
function addListener(event,callback) {
g_eventEmitter.on(event,callback);
}
function removeListener(event,callback) {
g_eventEmitter.removeListener(event,callback);
}
function addChangeListener(callback) {
g_eventEmitter.on(CHANGE_EVENT,callback);
}
function removeChangeListener(callback) {
g_eventEmitter.removeListener(CHANGE_EVENT,callback);
}
function _onCentralState(state,tag) {
g_isReady = true;
const is_powered_on = !!state.isPoweredOn;
if (g_isPoweredOn != is_powered_on) {
g_isPoweredOn = is_powered_on;
g_eventEmitter.emit(CHANGE_EVENT,tag);
} else if (tag == 'startup') {
g_eventEmitter.emit(CHANGE_EVENT,tag);
}
}
function _onPeripheralCountUpdate(count,tag) {
if (g_connectedPeripheralCount != count) {
g_connectedPeripheralCount = count;
g_eventEmitter.emit(CHANGE_EVENT,tag);
}
}
function _onPeripheralRead(data) {
const opts = {
requestId: data.requestId,
value: "1234",
};
BluetoothModule.respondToRequest(opts,(err) => {
console.log("Bluetooth._onPeripheralRead: respondToRequest err:",err);
});
}
function _onPeripheralWrite(data) {
const { valueBase64 } = data;
const buf = new Buffer(valueBase64,'base64');
data.valueBuffer = buf;
data.value = buf.toString('utf8');
g_eventEmitter.emit("write",data);
}
function _onPeripheralUpdateValue(data) {
const { valueBase64 } = data;
const buf = new Buffer(valueBase64,'base64');
data.valueBuffer = buf;
data.value = buf.toString('utf8');
g_eventEmitter.emit("updateValue",data);
}
function _checkPeripherials() {
BluetoothModule.getPeripheralList((err,peripheral_list) => {
let count = 0;
peripheral_list.forEach((p) => {
if (p.isConnected) {
count++;
} else if (!p.isConnecting) {
BluetoothModule.connectToPeripheral(p.peripheral,(err) => {});
}
});
_onPeripheralCountUpdate(count);
});
}
function getConnectedPeripheralCount() {
return g_connectedPeripheralCount;
}
function isReady() {
return g_isReady;
}
function isPoweredOn() {
return g_isPoweredOn;
}
function startScanning() {
BluetoothModule.startScanning((err) => {
console.log("Bluetooth.startScanning: err:",err);
});
}
function sendValue(value,done = function() {}) {
if (Buffer.isBuffer(value)) {
value = value.toString('base64');
} else {
value = new Buffer(value,'utf8').toString('base64');
}
BluetoothModule.notifyWithValue(value,done);
}
export default {
init,
addListener,
removeListener,
addChangeListener,
removeChangeListener,
getConnectedPeripheralCount,
isReady,
isPoweredOn,
startScanning,
sendValue,
};