Android7.0 获取蓝牙设备电量

参考http://blog.csdn.net/jcxxxxx55/article/details/52847291?locationNum=4&fps=1

1.修改 HeadsetStateMachine
packages/apps/Bluetooth/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
BluetoothAssignedNumbers.GOOGLE随便用哪个公司的,记得和注册广播那里一直。
processVendorSpecificAt可以去看看,如果还有哪些at command不支持的话,可以再添加。

    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);
//aaron
Log.e("a", "put IPHONEACCEV " );
        VENDOR_SPECIFIC_AT_COMMAND_COMPANY_ID.put("+XAPL", BluetoothAssignedNumbers.GOOGLE);
        VENDOR_SPECIFIC_AT_COMMAND_COMPANY_ID.put("+IPHONEACCEV", BluetoothAssignedNumbers.GOOGLE);
    }

2.注册广播BluetoothSettings
packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothSettings.java
在settings中的蓝牙界面中注册广播,然后把电量显示出来。

        //aaron
        IntentFilter filter=new IntentFilter();
        filter.addAction(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
        //filter.addCategory(BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY+"."+BluetoothAssignedNumbers.GOOGLE);
        filter.addCategory(BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY+"."+BluetoothAssignedNumbers.GOOGLE);
        getActivity().registerReceiver(mIntentReceiver,filter);
        Log.i("a", "registerReceiver  ");
        //end


    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT)) {
                Log.i("a", "intent  "+intent);
                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 >= 3 && args[0] instanceof Integer && ((Integer)args[0])*2+1<=args.length) {
                        for (int i=0;i<((Integer)args[0]);i++) {
                            if (!(args[i*2+1] instanceof Integer) || !(args[i*2+2] instanceof Integer)) {
                                continue;
                            }
                            if (args[i*2+1].equals(1)) {
                                float level = (((Integer)args[i*2+2])+1)/10.0f;
                                Log.i("a", "battery   "+level);
                                break;
                            }
                        }
                    }
                }
            }
        }
    };

附上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

没有更多推荐了,返回首页