天天看點

Android狀态欄顯示藍牙耳機電量

1.HeadsetStateMachine.java

static {
        classInitNative();
        VENDOR_SPECIFIC_AT_COMMAND_COMPANY_ID = new HashMap<String, Integer>();
        VENDOR_SPECIFIC_AT_COMMAND_COMPANY_ID.put("+XEVENT", BluetoothAssignedNumbers.PLANTRONICS);
        VENDOR_SPECIFIC_AT_COMMAND_COMPANY_ID.put("+ANDROID", BluetoothAssignedNumbers.GOOGLE);
// 添加以下兩行
        VENDOR_SPECIFIC_AT_COMMAND_COMPANY_ID.put("+XAPL", BluetoothAssignedNumbers.APPLE);
        VENDOR_SPECIFIC_AT_COMMAND_COMPANY_ID.put("+IPHONEACCEV", BluetoothAssignedNumbers.APPLE);
    }
           

2.PhoneStatusBarPolicy.java

public PhoneStatusBarPolicy(Context context, CastController cast, HotspotController hotspot,
            UserInfoController userInfoController, BluetoothController bluetooth) {
filter.addAction(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
            filter.addCategory(BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY + "." + BluetoothAssignedNumbers.APPLE);
                mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);

    }

    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 添加如下接收
            else if (action.equals(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT)) {
                updateBluetoothBattery(intent);
            }
        }
    };

    private void updateBluetoothBattery(Intent intent) {
        if (intent.hasExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD)) {
            String command = intent.getStringExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD);
            if ("+IPHONEACCEV".equals(command)) {
                Object[] args = (Object[]) intent.getSerializableExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS);
                if (args.length >=  && args[] instanceof Integer && ((Integer)args[])*+<=args.length) {
                    for (int i=;i<((Integer)args[]);i++) {
                        if (!(args[i*+] instanceof Integer) || !(args[i*+] instanceof Integer)) {
                            continue;
                        }
                        if (args[i*+].equals()) {
                            mBluetoothBatteryLevel = (((Integer)args[i*+])+)/f;
                            updateBluetooth();
                            break;
                        }
                    }
                }
            }
        }
    }

    private final void updateBluetooth() {
        int iconId = R.drawable.stat_sys_data_bluetooth;
        String contentDescription =
                mContext.getString(R.string.accessibility_quick_settings_bluetooth_on);
        boolean bluetoothEnabled = false;
        if (mBluetooth != null) {
            bluetoothEnabled = mBluetooth.isBluetoothEnabled();
            if (mBluetooth.isBluetoothConnected()) {
                if (mBluetoothBatteryLevel == null) {
                    iconId = R.drawable.stat_sys_data_bluetooth_connected;
                } else {
                    if (mBluetoothBatteryLevel <= f) {
                        iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_0;
                    } else if (mBluetoothBatteryLevel <= f) {
                        iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_1;
                    } else if (mBluetoothBatteryLevel <= f) {
                        iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_2;
                    } else if (mBluetoothBatteryLevel <= f) {
                        iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_3;
                    } else if (mBluetoothBatteryLevel <= f) {
                        iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_4;
                    } else {
                        iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_5;
                    }
                }
                contentDescription = mContext.getString(R.string.accessibility_bluetooth_connected);
            } else {
                mBluetoothBatteryLevel = null;
            }
        }

        mService.setIcon(SLOT_BLUETOOTH, iconId, , contentDescription);
        mService.setIconVisibility(SLOT_BLUETOOTH, bluetoothEnabled);
    }
           

如上,藍牙耳機一般會針對 iOS 做定制。而沒有針對Android做定制。

附上HFP指令AT+IPHONEACCEV

描述:報告耳機的狀态變更
發起者:耳機
格式:AT+IPHONEACCEV=[Number of key/value pairs ],[key1 ],[val1 ],[key2 ],[val2 ],...
參數:
    Number of key/value pairs : 接下來參數的數量
    key: 被報告狀态變化的類型
        1 = 電量等級
        2 = 暫停狀态
    val: 更改的值
        Battery events:0-9之間數字的字元串 A string value between '0' and '9'.
        Dock state: 0 = undocked, 1 = docked.
Example: AT+IPHONEACCEV=1,1,3
           

繼續閱讀