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-ble-manager
####iOS
- Open the node_modules/react-native-ble-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-ble-manager'
project(':react-native-ble-manager').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-ble-manager/android')
#####Update Gradle Build
// file: android/app/build.gradle
...
dependencies {
...
compile project(':react-native-ble-manager')
}
#####Register React Package
...
import it.innove.BleManagerPackage; // <--- import
public class MainActivity extends ReactActivity {
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new BleManagerPackage(this) // <------ add the package
);
}
...
}
##Methods
Scan for availables peripherals.
Returns a Promise
object.
Arguments
serviceUUIDs
-Array of String
- the UUIDs of the services to looking for.seconds
-Integer
- the amount of seconds to scan.
Examples
BleManager.scan([], 5)
.then(() => {
// Success code
console.log('Scan started');
});
Attempts to connect to a peripheral.
Returns a Promise
object.
Arguments
peripheralId
-String
- the id/mac address of the peripheral to connect.
Examples
BleManager.connect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
.then(() => {
// Success code
console.log('Connected');
})
.catch((error) => {
// Failure code
console.log(error);
});
Disconnect from a peripheral.
Returns a Promise
object.
Arguments
peripheralId
-String
- the id/mac address of the peripheral to disconnect.
Examples
BleManager.disconnect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
.then(() => {
// Success code
console.log('Disconnected');
})
.catch((error) => {
// Failure code
console.log(error);
});
Force the module to check the state of BLE and trigger a BleManagerDidUpdateState event.
Examples
BleManager.checkState();
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.startNotification('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
.then(() => {
// Success code
console.log('Notification started');
})
.catch((error) => {
// Failure code
console.log(error);
});
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 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.read('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
.then((readData) => {
// Success code
console.log('Read: ' + readData);
})
.catch((error) => {
// Failure code
console.log(error);
});
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.
To get the data
into base64 format, you will need buffer
, which doesn't port natively on iOS. Install rn-nodeify
like so:
npm install -g rn-nodeify
And then install buffer
using rn-nodeify
using its hack
flag:
rn-nodeify --install "buffer" --hack
Finally, to format the string before calling the write function:
var data = Buffer("data string").toString('base64');
##Events
The scanning for peripherals is ended.
Arguments
none
Examples
NativeAppEventEmitter.addListener(
'BleManagerStopScan',
() => {
// Scanning is stopped
}
);
The BLE change state.
Arguments
state
-String
- the new BLE state ('on'/'off').
Examples
NativeAppEventEmitter.addListener(
'BleManagerDidUpdateState',
(args) => {
// The new state: args.state
}
);
The scanning find a new peripheral.
Arguments
id
-String
- the id of the peripheralname
-String
- the name of the peripheral
Examples
NativeAppEventEmitter.addListener(
'BleManagerDiscoverPeripheral',
(args) => {
// The id: args.id
// The name: args.name
}
);
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
A peripheral is disconnected.
Arguments
peripheral
-String
- the id of the peripheral