android studio aar包导入
1.aar包放入\app\libs下
2.build.gradle添加以下:
repositories{
flatDir {
dirs 'libs'
}
}
3.dependencies添加:
compile(name:'blemulti-release', ext:'aar')
4.添加第三方库:
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'io.reactivex:rxandroid:1.2.1'
5.在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
6.在Application中初始化:
Rat.initilize(this);
关于蓝牙的相关操作
1.启用蓝牙
Rat.getInstance().enableBluetooth();
2.关闭蓝牙
Rat.getInstance().disableBluetooth();
3.扫描蓝牙
Scan_CB scanCallback = new Scan_CB() {
@Override
public void onLeScan(BluetoothDevice device) {
if (!TextUtils.isEmpty(device.getName())) {
mAdapter.addItem(new BleScanAdapter.Item(device.getAddress(), device.getName()));
}
}
};
Rat.getInstance().startScan(scanCallback);
4.停止扫描
Rat.getInstance().stopScan();
5.建立连接
Rat.getInstance().connectDevice_Normal(item.getMac(), new SucFail() {
@Override
public void onSucceed_UI(String msg) {
connSuccess(item);
}
@Override
public void onFailed_UI(String msg) {
connFail();
}
}
);
6.获得连接上的蓝牙设备
BleDevice bleDevice = Rat.getInstance().getFirstBleDevice();
7.断开蓝牙连接
List<BleDevice> list = Rat.getInstance().getConnectedDevice();
BleDevice device = list.get(which);
Rat.getInstance().disConnectDevice(device.get_macAddress());
8.观察蓝牙的状态:
Rat.getInstance().Obserable_Ble()
.subscribe(new Action1<OB_Ble>() {
@Override
public void call(OB_Ble ob_ble) {
if (ob_ble == OB_Ble.ON) {
// 蓝牙打开的状态
}else if (ob_ble == OB_Ble.OFF) {
// 蓝牙关闭的状态
} else if (ob_ble == OB_Ble.Conn_No_GPS) {
// 手机的gps没有启用(6.0后的蓝牙操作需要gps)
} else if (ob_ble == OB_Ble.Conn_No_Location) {
// 手机的位置没有启用
}
}
});
关于采集测温枪传上来的数据的相关操作
Recipe.newInstance(bleDevice)
.getValue_Z( isBtnFirst, new Value_CB() {
@Override
public void onValue_UI(float x, float y, float z) {
// z为测温枪传输过来的温度值
}
@Override
public void onFail_UI(@NonNull BleException e) {
//失败处理
}
});
@isBtnFirst采集按钮:
第一次按下采集按钮的时候:isBtn=true
按下后的后续采集:isBtn=false
网友评论