Android 测试手机与智能血压计BLE蓝牙扫描、连接、认证、断开的代码
时间: 2023-07-10 21:09:33 浏览: 174
BLEDemo.txt
以下是 Android 测试手机与智能血压计 BLE 蓝牙扫描、连接、认证、断开的代码示例,供参考:
首先,在 Manifest 中添加以下权限:
```xml
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
```
然后,在代码中实现 BLE 相关功能:
```java
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mBluetoothLeScanner;
private ScanCallback mScanCallback;
private BluetoothGatt mBluetoothGatt;
// 初始化 BLE 相关对象
private void initBluetooth() {
// 获取蓝牙适配器
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 获取蓝牙扫描器
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
}
// 扫描 BLE 设备
private void startScan() {
// 构建扫描过滤器
ScanFilter scanFilter = new ScanFilter.Builder()
.setDeviceName("BloodPressure") // 设置设备名称
.setServiceUuid(ParcelUuid.fromString("00001810-0000-1000-8000-00805f9b34fb")) // 设置服务 UUID
.build();
// 构建扫描设置
ScanSettings scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // 设置扫描模式
.build();
// 构建扫描回调
mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
// 获取设备地址
String deviceAddress = result.getDevice().getAddress();
// 停止扫描
stopScan();
// 连接设备
connectDevice(deviceAddress);
}
};
// 开始扫描
mBluetoothLeScanner.startScan(Collections.singletonList(scanFilter), scanSettings, mScanCallback);
}
// 停止扫描 BLE 设备
private void stopScan() {
mBluetoothLeScanner.stopScan(mScanCallback);
}
// 连接 BLE 设备
private void connectDevice(String deviceAddress) {
// 获取设备对象
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
// 连接设备
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
}
// GATT 回调
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
// 连接成功,开始认证
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// 断开连接
gatt.close();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
// 获取服务对象
BluetoothGattService service = gatt.getService(UUID.fromString("00001810-0000-1000-8000-00805f9b34fb"));
if (service != null) {
// 获取特征对象
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString("00002a35-0000-1000-8000-00805f9b34fb"));
if (characteristic != null) {
// 设置特征通知
gatt.setCharacteristicNotification(characteristic, true);
// 获取描述对象
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if (descriptor != null) {
// 设置描述通知
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
}
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
// 处理数据
byte[] data = characteristic.getValue();
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
// 认证成功,开始接收数据
if (status == BluetoothGatt.GATT_SUCCESS) {
// 发送认证指令
byte[] authCommand = {0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
BluetoothGattService service = gatt.getService(UUID.fromString("00001810-0000-1000-8000-00805f9b34fb"));
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString("00002a35-0000-1000-8000-00805f9b34fb"));
if (characteristic != null) {
characteristic.setValue(authCommand);
gatt.writeCharacteristic(characteristic);
}
}
}
}
};
// 断开 BLE 设备连接
private void disconnectDevice() {
if (mBluetoothGatt != null) {
mBluetoothGatt.disconnect();
}
}
```
以上代码实现了 BLE 设备的扫描、连接、认证和断开连接功能。其中,扫描时需要设置设备名称和服务 UUID,连接后需要发起服务发现、设置特征通知和描述通知,认证时需要发送认证指令,数据接收操作在 `onCharacteristicChanged` 方法中进行处理。
阅读全文