react-native-simple-ble v0.0.1
react-native-simple-ble
Currently only support ios and require react-native >= 0.40.0
##Installation
1. Install library from npm
```bash
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
scan(UUIDs,allowDuplicates,showPowerAlert)
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']);stopScan()
Stop the scanning.
Examples
ble.stopScan();connect(identifier)
Connect to a specified peripheral.
Arguments
identifier-String- Using the method you have to obtain theidentifierbefore ,from methodregisterDiscoverPeripheral().
Examples
ble.connect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')disconnect()
Disconnect from a peripheral.
Examples
ble.disconnect();write(msg,UUID)
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(UUID)
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.
Events
registerUpdateConnectionStatus(callback)
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.
registerDiscoverPeripheral()
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)
registerDidUpdateValueForCharacteristic()
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.
Simple ble api for react nativeparam.value-String- Characteristic value.f001669d457ad68bec621814dff0742bdf3d892b
9 years ago