Make sure your project has been introduced RxJava in build.gradle
,like this:
compile 'io.reactivex:rxjava:1.2.3'
compile 'io.reactivex:rxandroid:1.2.1'
And copy RxBle.java
to your project.
1.Get the singleton of the RxBle
RxBle rxBle = RxBle.getInstance()
or if you know the target BLE device name such as Test
RxBle rxBle = RxBle.getInstance().setTargetDevice("Test")
2.Open bluetooth by no asking
mRxBle.openBle(this);
3.If you did not use setTargetDevice
to set target device name,you should set a listener in RxBle to get a list of devices when scanning,then connect the target device by yourself
rxBle.setScanListener(new RxBle.BleScanListener() {
@Override
public void onBleScan(BluetoothDevice bleDevice, int rssi, byte[] scanRecord) {
// Get list of devices and other information
if(bledevice.getName().equals("Test")){
rxBle.connectDevice(bleDevice);
}
}
});
4.Communication between Android phone and BLE device
rxBle.sendData(data);
// or send with delay
rxBle.sendData(data,delay);
rxBle.receiveData().subscribe(new Action1<String>() {
@Override
public void call(String receiveData) {
// Data will be received while they sent by BLE device
}
});
5.Close BLE
rxBle.closeBle();
The best practice is fork and edit it,adapt to your project needs.
- Twitter:@qiantao94
- Gmail:qiantao94@gmail.com