react-native-blue-manager
This is a porting of https://github.com/don/cordova-plugin-ble-central project to React Native.
Supported Platforms
- iOS
- Android (API 18)
Install
npm i --save react-native-blue-manager
iOS
- Open the node_modules/react-native-blue-manager/ios folder and drag BleManager.xcodeproj into your Libraries group.
- Check the "Build Phases"of your project and add "libBleManager.a" in the "Link Binary With Libraries" section.
Android
Update Gradle Settings
// file: android/settings.gradle... include ':react-native-blue-manager'project(':react-native-blue-manager').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-blue-manager/android')
Update Gradle Build
// file: android/app/build.gradle... dependencies { ... compile project(':react-native-blue-manager')}
Register React Package
... // <--- import
Basic Example
{ super thisstate = ble:null scanning:false } { thishandleDiscoverPeripheral = thishandleDiscoverPeripheral; NativeAppEventEmitter ; } { BleManager ; } { if bool this thisscanning = ; else this ; } { console; this } { const container = flex: 1 justifyContent: 'center' alignItems: 'center' backgroundColor: '#F5FCFF' const bleList = thisstateble ? <Text> Device found: thisstateblename </Text> : <Text>no devices nearby</Text> return <View style=container> <TouchableHighlight style=padding:20 backgroundColor:'#ccc' onPress= this > <Text>Scan </Text> </TouchableHighlight> bleList </View> ; }
Methods
scan(serviceUUIDs, seconds)
Scan for availables peripherals.
Returns a Promise
object.
Arguments
serviceUUIDs
-Array of String
- the UUIDs of the services to looking for. On Android the filter works only for 5.0 or newer.seconds
-Integer
- the amount of seconds to scan.allowDuplicates
-Boolean
- [iOS only] allow duplicates in device scanning
Examples
BleManager ;
stopScan()
Stop the scanning.
Returns a Promise
object.
Examples
BleManager ;
connect(peripheralId)
Attempts to connect to a peripheral.
Returns a Promise
object.
Arguments
peripheralId
-String
- the id/mac address of the peripheral to connect.
Examples
BleManager ;
disconnect(peripheralId)
Disconnect from a peripheral.
Returns a Promise
object.
Arguments
peripheralId
-String
- the id/mac address of the peripheral to disconnect.
Examples
BleManager ;
checkState()
Force the module to check the state of BLE and trigger a BleManagerDidUpdateState event.
Examples
BleManager;
startNotification(peripheralId, serviceUUID, characteristicUUID)
Start the notification on the specified characteristic.
Returns a Promise
object.
Arguments
peripheralId
-String
- the id/mac address of the peripheral.serviceUUID
-String
- the UUID of the service.characteristicUUID
-String
- the UUID of the characteristic.
Examples
BleManager ;
stopNotification(peripheralId, serviceUUID, characteristicUUID)
Stop the notification on the specified characteristic.
Returns a Promise
object.
Arguments
peripheralId
-String
- the id/mac address of the peripheral.serviceUUID
-String
- the UUID of the service.characteristicUUID
-String
- the UUID of the characteristic.
read(peripheralId, serviceUUID, characteristicUUID)
Read the current value of the specified characteristic.
Returns a Promise
object.
Arguments
peripheralId
-String
- the id/mac address of the peripheral.serviceUUID
-String
- the UUID of the service.characteristicUUID
-String
- the UUID of the characteristic.
Examples
BleManager ;
write(peripheralId, serviceUUID, characteristicUUID, data, maxByteSize)
Write with response to the specified characteristic.
Returns a Promise
object.
Arguments
peripheralId
-String
- the id/mac address of the peripheral.serviceUUID
-String
- the UUID of the service.characteristicUUID
-String
- the UUID of the characteristic.data
-String
- the data to write in Base64 format.maxByteSize
-Integer
- specify the max byte size before splitting message
To get the data
into base64 format, you will need a library like base64-js
. Install base64-js
:
npm install base64-js --save
To format the data before calling the write function:
var base64 = ;var data = base64;
Examples
BleManager ;
writeWithoutResponse(peripheralId, serviceUUID, characteristicUUID, data, maxByteSize)
Write without response to the specified characteristic.
Returns a Promise
object.
Arguments
peripheralId
-String
- the id/mac address of the peripheral.serviceUUID
-String
- the UUID of the service.characteristicUUID
-String
- the UUID of the characteristic.data
-String
- the data to write in Base64 format.maxByteSize
-Integer
- specify the max byte size
To get the data
into base64 format, you will need a library like base64-js
. Install base64-js
:
npm install base64-js --save
To format the data before calling the write function:
var base64 = ;var data = base64;
Examples
BleManager ;
getConnectedPeripherals(serviceUUIDs)
Return the connected peripherals.
Returns a Promise
object.
Arguments
serviceUUIDs
-Array of String
- the UUIDs of the services to looking for.
Examples
BleManager ;
getDiscoveredPeripherals()
Return the discovered peripherals after a scan.
Returns a Promise
object.
Examples
BleManager ;
isPeripheralConnected(peripheralId, serviceUUIDs)
Check whether a specific peripheral is connected and return true
or false
.
Returns a Promise
object.
Examples
BleManager ;
Events
BleManagerStopScan
The scanning for peripherals is ended.
Arguments
none
Examples
NativeAppEventEmitter;
BleManagerDidUpdateState
The BLE change state.
Arguments
state
-String
- the new BLE state ('on'/'off').
Examples
NativeAppEventEmitter;
BleManagerDiscoverPeripheral
The scanning find a new peripheral.
Arguments
id
-String
- the id of the peripheralname
-String
- the name of the peripheral
Examples
NativeAppEventEmitter;
BleManagerDidUpdateValueForCharacteristic
A characteristic notify a new value.
Arguments
peripheral
-String
- the id of the peripheralcharacteristic
-String
- the UUID of the characteristicvalue
-String
- the read value in Hex format
BleManagerConnectPeripheral
A peripheral was connected.
Arguments
peripheral
-String
- the id of the peripheral
BleManagerDisconnectPeripheral
A peripheral was disconnected.
Arguments
peripheral
-String
- the id of the peripheral