Simple BLE api for react native
Currently only support ios and require react-native >= 0.40.0.
##Installation
-
Install library from
npm
npm install react-native-simple-ble --save
-
Link native code
react-native link react-native-simple-ble
##Usage
import ble from 'react-native-simple-ble'
##Methods
Scaning peripherals.
See method registerDiscoverPeripheral()
.
Arguments The parameter is optional the configuration keys are:
UUIDs
-Array of String
- [default empty] If you confirm the service UUID in the broadcast information,fill the UUID,block out you don't want, otherwise not.allowDuplicates
-Boolean
- [default false] Allow scanning duplicates.showPowerAlert
-Boolean
- [default true] Show or hide the alert if the bluetooth is turned off during initialization.
Examples
ble.scan();
// ble.scan(['XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX']);
Stop the scanning.
Examples
ble.stopScan();
Connect to a specified peripheral.
Arguments
identifier
-String
- Using the method you have to obtain theidentifier
before ,from methodregisterDiscoverPeripheral()
.
Examples
ble.connect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
Disconnect from a peripheral.
Examples
ble.disconnect();
Write to the specified characteristic.
Returns a Promise
object.
Arguments
msg
-String
- The data to write hexstring.(e.g. 'ABCDEF0123456789')UUID
-String
- [default empty] If you confirm the write more than one characteristic,fill in your specified write characteristic UUID , otherwise not.
Examples
// ble.write('ABCDEF0123456789')
ble.write('ABCDEF0123456789','XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
.then((result) =>{
console.log(result.writeUUID+' : ' +result.message);
})
.catch((error) =>{
console.log(error);
})
result.writeUUID
-String
- write characteristic UUID.result.message
-String
- write state message.error
- error.
Read value for the specified characteristic.
Returns a Promise
object.
Arguments
UUID
-String
- the UUID of the read characteristic.
Examples
ble.read('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
.then((result) =>{
console.log(result.readUUID+' : '+result.value);
})
.catch((error)=>{
console.log(error);
})
result.writeUUID
-String
- read characteristic UUID.result.value
-String
- characteristic value.error
- error.
Register a callback to monitor the connection status.
The status changed callback.
Examples
ble.registerUpdateConnectionStatus((param)=>{
console.log(param.connect+','+param.desc);
});
param.connect
-Boolean
- Is connect or not.param.desc
-String
- Error message description.
Register a callback to monitor discover peripherals.see method connect()
.
Once found a peripheral callback.
Examples
ble.registerDiscoverPeripheral((param)=>{
console.log(param.name+','+param.identifier);
});
param.name
-String
- Peripheral's name.param.identifier
-String
- Peripheral's identifier.Using connection,see methodconnect()
.param.RSSI
-Number
- Peripheral received signal strength indicator....
- (broadcast information)
Register a callback to monitor characteristic value.
Characteristic value did update callback.You can get all peripheral sending data from here.
Examples
ble.registerDidUpdateValueForCharacteristic((param)=>{
console.log(param.UUID+' : ',param.value);
});
param.UUID
-String
- Characteristic UUID.param.value
-String
- Characteristic value.