#BleRx 对rxandroidble的操作封装,适用于只连一个设备,只要数据的收发等基本功能 ##配置 ####RxAndroidBle
compile "com.polidea.rxandroidble:rxandroidble:1.1.0"
####lambda语法配置
- 根目录build.gradle
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.1.0'
}
- 项目build.gradle
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
allprojects {
repositories {
maven {
url "https://jitpack.io"
}
}
apply plugin: 'me.tatarka.retrolambda'
}
}
##使用 直接复制BleRx到项目中使用 ###初始化 在Application的onCreate()中
BleRx.init(this);
###扫描
Subscription subscribe = BleRx.getInstance()
.scan()
.subscribe(result -> {
//扫描结果
}, e -> {
});
subscribe.unsubscribe();//取消扫描
###连接
BleRx.getInstance().connect("ff:ff:ff:ff:ff:ff", ff);
//mac地址,重连时间
###断开
BleRx.getInstance().disConnect();
###读
BleRx.getInstance().read(UUID).subscribe(bytes->{
});
###写
BleRx.getInstance().write(UUID,bytes);
###notify
Subscription subscribe = BleRx.getInstance().notifyData(UUID).subscribe(bytes -> {
});
subscribe.unsubscribe();//取消notify
###rssi
BleRx.getInstance().readRssi().subscribe(rssi->{
});
###蓝牙状态
Subscription subscribe = BleRx.getInstance().bleState().subscribe(state -> {
});
subscribe.unsubscribe();//取消状态监听