蓝牙耳机电量显示
来源:互联网 发布:淘宝运费模板怎么写 编辑:程序博客网 时间:2024/05/02 02:44
方案1只在连接耳机时获取到信息
方案2为:隔10%——20%左右接收到蓝牙耳机信息
方案1:
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
import android.bluetooth.BluetoothAssignedNumbers;import android.bluetooth.BluetoothHeadset; private Float mBluetoothBatteryLevel;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 >= 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)) { mBluetoothBatteryLevel = (((Integer)args[i*2+2])+1)/10.0f; 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 <= 0.15f) { iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_0; } else if (mBluetoothBatteryLevel <= 0.25f) { iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_1; } else if (mBluetoothBatteryLevel <= 0.375f) { iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_2; } else if (mBluetoothBatteryLevel <= 0.625f) { iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_3; } else if (mBluetoothBatteryLevel <= 0.85f) { 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, 0, 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=2,1,4,2,02:两个参数1,4 key值1代表电量:42,0 key值2代表:undocked
附上HFP Command AT+XAPL
HFP命令AT+XAPL
描述:允许通过耳机自定义AT命令
发起者:耳机
格式:AT+XAPL=[vendorID]-[productID]-[version],[features]
参数:
vendorID: 标识生产商的vendor ID的十六进制表示,但是没有0x前缀
productID: 标识生产生的product ID的十六进制表示,但是没有0x前缀
version: 软件的版本
features: 用10进制标识的位标识:
1 = 耳机支持电池电量报告
2 = 耳机暂停或者正在充电
其他值保留
例子: AT+XAPL=ABCD-1234-0100,3
响应: +XAPL=iPhone,[features]
方案2:
1.在HeadsetStateMachine.java文件中processUnknownAt方法中添加对此AT命令的处理函数如下:
else if (atCommand.startsWith("+IPHONEACCEV")){ processAtBattery(atString);}processAtBattery(atString)为将获取到的attring解析并将电量以广播形式发出
private void processAtBattery(String atString) { Log.i(TAG,"BlueToothBattery atString:" + atString);// +IPHONEACCEV=2,1,4,2,0 String[] args = atString.split("=")[1].split(","); if (args.length >= 3 && (Integer.parseInt(args[0])*2+1) <= args.length) { for (int i=0;i<Integer.parseInt(args[0]);i++) { if (args[i*2+1].equals("1")) { Float mBluetoothBatteryLevel = (Integer.parseInt(args[i*2+2])+1)/10.0f; Intent intent = new Intent(); intent.setAction("action.bluetooth.battery.refresh"); intent.putExtra("BluetoothBatteryLevel",mBluetoothBatteryLevel); mService.sendBroadcast(intent); break; } } }}2.PhoneStatusBarPolicy.java中接收广播并做显示处理
filter.addAction("action.bluetooth.battery.refresh");
else if(action.equals("action.bluetooth.battery.refresh")){ Float mBluetoothBatteryLevel = intent.getFloatExtra("BluetoothBatteryLevel",0); updateBluetoothBattery(mBluetoothBatteryLevel);}
private final void updateBluetoothBattery(Float mBluetoothBatteryLevel) { int iconId = R.drawable.stat_sys_data_bluetooth; String contentDescription = mContext.getString(R.string.accessibility_quick_settings_bluetooth_on); boolean bluetoothEnabled = false; Log.d(TAG, "mBluetooth = " + mBluetooth + " mBluetooth.isBluetoothConnected():" + mBluetooth.isBluetoothConnected() + " mBluetoothBatteryLevel:" + mBluetoothBatteryLevel); if (mBluetooth != null) { bluetoothEnabled = mBluetooth.isBluetoothEnabled(); if (mBluetooth.isBluetoothConnected()) { if (mBluetoothBatteryLevel == null) { iconId = R.drawable.stat_sys_data_bluetooth_connected; } else { if (mBluetoothBatteryLevel <= 0.20f) { iconId = R.drawable.stat_sys_bluetooth_battery_1; } else if (mBluetoothBatteryLevel <= 0.40f) { iconId = R.drawable.stat_sys_bluetooth_battery_2; } else if (mBluetoothBatteryLevel <= 0.60f) { iconId = R.drawable.stat_sys_bluetooth_battery_3; } else if (mBluetoothBatteryLevel <= 0.80f) { iconId = R.drawable.stat_sys_bluetooth_battery_4; } else { iconId = R.drawable.stat_sys_bluetooth_battery_5; } } contentDescription = mContext.getString(R.string.accessibility_bluetooth_connected); } else { mBluetoothBatteryLevel = null; } } mService.setIcon(SLOT_BLUETOOTH, iconId, 0, contentDescription); mService.setIconVisibility(SLOT_BLUETOOTH, bluetoothEnabled);}
阅读全文
0 0
- 蓝牙耳机电量显示
- Android状态栏显示蓝牙耳机电量
- iphone 显示蓝牙电量
- 蓝牙耳机图标显示过程
- 蓝牙耳机
- 蓝牙耳机
- 蓝牙耳机
- 蓝牙耳机
- 使用蓝牙耳机
- 蓝牙耳机复位
- 连接蓝牙耳机过程
- 蓝牙耳机录音
- windows10连接蓝牙耳机
- iOS 蓝牙耳机切换
- 蓝牙耳机录音
- android 判断有线耳机、蓝牙耳机连接
- nrf51822蓝牙之电量检测
- 蓝牙耳机与手机相连
- equals() 和 hashCode() 区别
- [译]Go net/http 超时机制完全手册
- MongoDB 修改认证机制
- Docker1-入门
- Android native层事件传递 1
- 蓝牙耳机电量显示
- java23种设计模式(2)-责任链模式
- Flex中的DataGrid
- hdu 1060(思维)
- jiecaovideoplayer的简单使用
- filebeat传输到logstash通过ssl加密
- CentOS搭建SVN服务器
- Python入门与进阶学习笔记之分支、循环、条件与枚举
- 钉钉生成jspapi token出现IP地址不在白名单的解决办法