react-native-ble-manager
This is a porting of https://github.com/don/cordova-plugin-ble-central project to React Native.
##Supported Platforms
##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(serviceUUIDs, seconds)
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([])
.then(() => {
console.log('Scan started');
});
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.connect('4B28EF69-423D-FA86-01FA-CC6CB923A2C9')
.then(() => {
console.log('Connected');
})
.catch((error) => {
console.log(error);
});
disconnect(peripheralId)
Disconnect from a peripheral.
Returns a Promise
object.
Arguments
peripheralId
- String
- the id/mac address of the peripheral to disconnect.
Examples
BleManager.disconnect('4B28EF69-423D-FA86-01FA-CC6CB923A2C9')
.then(() => {
console.log('Connected');
})
.catch((error) => {
console.log(error);
});
##Events
BleManagerStopScan
The scanning for peripherals is ended.
Arguments
BleManagerDidUpdateState
The BLE change state.
BleManagerDiscoverPeripheral
BleManagerDidUpdateValueForCharacteristic
BleManagerDisconnectPeripheral