forked from rusel1989/react-native-bluetooth-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (31 loc) · 1 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
const ReactNative = require('react-native')
const { Buffer } = require('buffer')
const { NativeModules, Platform } = ReactNative
const BluetoothSerial = NativeModules.BluetoothSerial
let EventEmitter
if (Platform.OS === 'android') {
EventEmitter = ReactNative.DeviceEventEmitter
} else {
EventEmitter = ReactNative.NativeAppEventEmmiter
}
/**
* Listen for available events
* @param {String} eventName Name of event one of connectionSuccess, connectionLost, data, rawData
* @param {Function} handler Event handler
*/
BluetoothSerial.on = (eventName, handler) => {
EventEmitter.addListener(eventName, handler)
}
/**
* Write data to device, you can pass string or buffer,
* We must convert to base64 in RN there is no way to pass buffer directly
* @param {Buffer|String} data
* @return {Promise<Boolean>}
*/
BluetoothSerial.write = (data) => {
if (typeof data === 'string') {
data = new Buffer(data)
}
return BluetoothSerial.writeToDevice(data.toString('base64'))
}
module.exports = BluetoothSerial