最近在开发鱼跃YE680A蓝牙血压计,获取测量到的血压和心率数据,之前也做有一个爱奥乐蓝牙血压计的,连接流程是一样的,在收发数据时有一些区别,下面做一些笔记:
一、整体的可定义三个类,一个Bluetooth服务的,用于连接,一个包含各类UUID的,用于收发数据,最后一个封装工具类,给调用者调用。
1. 先说连接吧,判断是不是支持蓝牙功能,是否打开蓝牙和请求打开蓝牙涉及到的,代码详细可能没贴出来了,网上一搜大把的。
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mBluetoothLeScanner;
private boolean isScanning; // 是否正在搜索
private void initBluetooth() {
BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if(mBluetoothAdapter == null) {
app.showToast(R.string.msg_bluetooth_no_support);
return;
} else {
if(!mBluetoothAdapter.isEnabled()) {
requestOpenBluetooth(); // 请求打开蓝牙
}
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
}
}
/**
* 开始搜索设备
* @param scan
*/
public void scanLeDevice(boolean scan) {
if(mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
requestOpenBluetooth(); // 请求打开蓝牙
return;
} else if(mBluetoothAdapter == null || mBluetoothLeScanner == null) {
MyLg.e("scanLeDevice", "mBluetoothLeScanner == null");
return;
}
if(scan) {
if(!isScanning) {
mBluetoothLeScanner.startScan(mScanCallback); // 开始搜索设备
}
isScanning = true;
} else {
mBluetoothLeScanner.stopScan(mScanCallback);
isScanning = false;
}
}
下面是扫描设备时的回调:
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(