第一步 首先添加权限,记得动态申请
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
第二步,获取配对的设备
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
textView.setText("不支持蓝牙");
// 设备不支持蓝牙
Log.e(TAG, "Device doesn't support Bluetooth");
} else {
if (!bluetoothAdapter.isEnabled()) {
Log.e(TAG, "open Bluetooth");
// 请求用户打开蓝牙
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 99);
} else {
textView.setText("蓝牙已打开");
getData();
}
}
private void getData() {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().contains("HUAWEI")) { //目标设备
// 假设我们找到了目标蓝牙耳机
getBatteryInfo(device);
}
}
} else {
tvContent.setText("no pair devices");
}
}
第三步,根据device获取电量,这里有两种方式
方式1,直接反射系统接口获取,但是获取的电量不是精确的,是两个耳机中电量最低的那个整数值,而且并不是实时的,每变化10%更新一次
private void getBatteryInfo(BluetoothDevice device) {
try {
Method getBatteryLevel = device.getClass().getMethod("getBatteryLevel");
int batteryLevel = (Integer) getBatteryLevel.invoke(device);
Log.e(TAG, "Battery Level: " + batteryLevel + "%");
showBatteryLevelToUser(batteryLevel);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "Failed to get battery level");
}
}
方式2,适用于支持ble蓝牙的设备,通过BluetoothGatt类去获取,这种方式需要知道电量uuid,虽然官方有默认的电量uuid,但是部分厂家会用自己定义的,电量特征的uuid如果是左右两个耳机,可能是不同的,需要分别获取电量值。
- 真无线耳机(TWS)通常将左右耳设计为两个独立的BLE设备,或通过单一设备的不同特征值(Characteristic)区分左右电量。
- 标准做法:左/右耳机电量分别存储在
Battery Service
下的不同特征值中(例如左耳UUID:00002a19-0000-1000-8000-00805f9b34fb
,右耳可能为自定义UUID)
private static final UUID MY_SERVICE_UUID = UUID.fromString("0000180F-0000-1000-8000-00805F9B34FB"); // 电池服务UUID
private static final UUID MY_CHARACTERISTIC_UUID = UUID.fromString("00002A19-0000-1000-8000-00805F9B34FB"); // 电池电量级别特性UUID
private void connectToDevice(BluetoothDevice device) {
@SuppressLint("MissingPermission")
BluetoothGatt bluetoothGatt = device.connectGatt(this, false, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) { // 成功连接
gatt.discoverServices(); // 成功连接
} else if (newState == BluetoothProfile.STATE_DISCONNECTED){
Log.e(TAG,"gatt disConnect");
} else {
Log.e(TAG,"gatt fail");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
List<BluetoothGattService> services = gatt.getServices();
Log.e(TAG,"BluetoothGattService size" + services.size());
BluetoothGattService batteryService = gatt.getService(MY_SERVICE_UUID); // 确认是电池服务
if (batteryService != null) {
BluetoothGattCharacteristic batteryLevelCharacteristic = batteryService.getCharacteristic(MY_CHARACTERISTIC_UUID); // 确认是电量特征,左右耳机可能不一样,需要分别获取,这里只获取一个
boolean success = gatt.readCharacteristic(batteryLevelCharacteristic); // 读取电量信息
if (success) {
Log.e(TAG,"gatt getBattery success");
// 等待 onCharacteristicRead回调来获取数据
} else {
// 读取失败处理
Log.e(TAG,"gatt getBattery fail");
}
} else {
Log.e(TAG, "batteryService is null");
}
} else {
// 服务发现失败处理
Log.e(TAG,"onServicesDiscovered fail");
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
int batteryLevel = characteristic.getValue()[0];// 直接转换为整数
Log.e(TAG, "Battery Level: " + batteryLevel + "%");
if (characteristic.getUuid().equals(“左边耳机的uuid”)) {
Log.d(TAG, "左耳电量: " + batteryLevel + "%");
} else if (characteristic.getUuid().equals(“右边耳机的uuid”)) { Log.d(TAG, "右耳电量: " + batteryLevel + "%");
}
// 将电量信息展示给用户
showBatteryLevelToUser(batteryLevel);
}
}
});
}