You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android平台用Java获取左右蓝牙耳机的电量

获取Android平台左右蓝牙耳机各自电量的解决方案

你之前使用的getBatteryLevel()方法只能返回蓝牙耳机主设备(通常是左耳)的电量,因为系统将TWS耳机识别为单个蓝牙设备,左右耳的电量需要通过BLE GATT服务读取,或者监听厂商自定义广播来获取,以下是具体实现方案:

方案一:通过BLE GATT服务读取(兼容性较好)

TWS耳机的左右耳通常作为独立的BLE子设备,各自提供电量相关的GATT特征。你需要连接到耳机的GATT服务,查找对应的电量特征UUID并读取值。

步骤1:连接GATT服务

首先通过BluetoothDevice连接到耳机的GATT服务:

BluetoothGatt gatt = device.connectGatt(context, false, new BluetoothGattCallback() {
    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 查找电池服务(通用UUID:0000180F-0000-1000-8000-00805F9B34FB)
            BluetoothGattService batteryService = gatt.getService(UUID.fromString("0000180F-0000-1000-8000-00805F9B34FB"));
            if (batteryService != null) {
                // 注意:不同厂商的左右耳电量特征UUID不同,需自行查找
                // 示例:替换为你实际找到的左右耳特征UUID
                BluetoothGattCharacteristic leftBattChar = batteryService.getCharacteristic(UUID.fromString("左耳电量特征UUID"));
                BluetoothGattCharacteristic rightBattChar = batteryService.getCharacteristic(UUID.fromString("右耳电量特征UUID"));
                
                // 读取特征值
                if (leftBattChar != null) {
                    gatt.readCharacteristic(leftBattChar);
                }
                if (rightBattChar != null) {
                    gatt.readCharacteristic(rightBattChar);
                }
            }
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            int batteryLevel = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
            UUID charUuid = characteristic.getUuid();
            
            // 根据特征UUID区分左右耳
            if (charUuid.equals(UUID.fromString("左耳电量特征UUID"))) {
                // 处理左耳电量,比如更新UI
                Log.d("Battery", "左耳电量:" + batteryLevel + "%");
            } else if (charUuid.equals(UUID.fromString("右耳电量特征UUID"))) {
                Log.d("Battery", "右耳电量:" + batteryLevel + "%");
            }
        }
    }

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            // 断开连接时释放资源
            gatt.close();
        }
    }
});

关键说明

  • 查找特征UUID:你需要用BLE调试工具(如nRF Connect)扫描耳机的蓝牙服务,找到左右耳各自的电量特征UUID,不同厂商的UUID差异较大。
  • 权限申请:需要在Manifest中添加蓝牙权限:
    <!-- Android 11及以下 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <!-- Android 12+ -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    

方案二:监听厂商自定义广播(依赖厂商实现)

部分厂商会通过自定义广播发送左右耳电量信息,你可以注册广播接收器监听:

BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // 不同厂商的广播Action可能不同,比如小米耳机用"com.xiaomi.bluetooth.headset.battery"
        if ("android.bluetooth.headset.action.BATTERY_LEVEL".equals(action)) {
            int leftBattery = intent.getIntExtra("left_battery", -1);
            int rightBattery = intent.getIntExtra("right_battery", -1);
            
            if (leftBattery != -1) {
                Log.d("Battery", "左耳电量:" + leftBattery + "%");
            }
            if (rightBattery != -1) {
                Log.d("Battery", "右耳电量:" + rightBattery + "%");
            }
        }
    }
};

// 注册广播接收器
IntentFilter filter = new IntentFilter("android.bluetooth.headset.action.BATTERY_LEVEL");
context.registerReceiver(batteryReceiver, filter);

注意:这种方法的兼容性较差,需要针对不同厂商的耳机适配广播Action和Extra字段。

内容的提问来源于stack exchange,提问作者S Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 11:22:49