androidQ(10.0) 读取蓝牙设备当前电量并显示
效果图
华为手机顶部状态栏
我们客制化后最终效果
实现步骤
1、获取蓝牙设备连接成功后的电量值
2、跟踪蓝牙图标显示流程
3、制作蓝牙带电量图标
4、获取电量后显示对应电量值图标
文件修改清单
vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable/ic_bluetooth_connected_super.xml vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable/stat_sys_data_bluetooth_connected_battery_0.xml vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable/stat_sys_data_bluetooth_connected_battery_1.xml vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable/stat_sys_data_bluetooth_connected_battery_2.xml vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable/stat_sys_data_bluetooth_connected_battery_3.xml vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable/stat_sys_data_bluetooth_connected_battery_4.xml vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable/stat_sys_data_bluetooth_connected_battery_5.xml vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
除了 PhoneStatusBarPolicy.java 之外其余都是新增蓝牙图标相关资源文件,资源文件不再贴出,可以去这里下载
资源文件选用 xml path 绘制,好处就是能跟随系统状态栏切换背景色,如果采用 png 图片将需要处理监听状态栏底色改变
读取已连接蓝牙设备电量值,在网上搜索后找到了一个工具类,简单试了下发现可用,通过反射调用 BluetoothDevice 的
getBatteryLevel 方法获取电量值
private int getBluetoothDeviceBattery(){int battery = -1;BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;try {Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);method.setAccessible(true);int state = (int) method.invoke(btAdapter, (Object[]) null);if (state == BluetoothAdapter.STATE_CONNECTED) {Set<BluetoothDevice> devices = btAdapter.getBondedDevices();for (BluetoothDevice device : devices) {Method batteryMethod = BluetoothDevice.class.getDeclaredMethod("getBatteryLevel", (Class[]) null);batteryMethod.setAccessible(true);Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);isConnectedMethod.setAccessible(true);boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);int level = (int) batteryMethod.invoke(device, (Object[]) null);if (device != null && level > 0 && isConnected) {String deviceName = device .getName();battery = level;android.util.Log.i(TAG, "Connected Bluetooth="+deviceName + " battery="+level);}}} else {android.util.Log.e(TAG, "No Connected Bluetooth Devices Found");}} catch (Exception e) {e.printStackTrace();}finally{return battery;}}
电量值为 -1 说明蓝牙设备不支持读取电量
其实后来通过跟踪系统蓝牙连接界面显示电量值逻辑,最终找到的也是调用此方法获取电量
frameworks\base\packages\SettingsLib\src\com\android\settingslib\bluetooth\CachedBluetoothDevice.java
/*** Return summary that describes connection state of this device. Summary depends on:* 1. Whether device has battery info* 2. Whether device is in active usage(or in phone call)** @param shortSummary {@code true} if need to return short version summary*/public String getConnectionSummary(boolean shortSummary) {boolean profileConnected = false; // Updated as long as BluetoothProfile is connectedboolean a2dpConnected = true; // A2DP is connectedboolean hfpConnected = true; // HFP is connectedboolean hearingAidConnected = true; // Hearing Aid is connectedint leftBattery = -1;int rightBattery = -1;.....String batteryLevelPercentageString = null;// Android framework should only set mBatteryLevel to valid range [0-100] or// BluetoothDevice.BATTERY_LEVEL_UNKNOWN, any other value should be a framework bug.// Thus assume here that if value is not BluetoothDevice.BATTERY_LEVEL_UNKNOWN, it must// be validfinal int batteryLevel = getBatteryLevel();if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) {// TODO: name com.android.settingslib.bluetooth.Utils something differentbatteryLevelPercentageString =com.android.settingslib.Utils.formatPercentage(batteryLevel);}int stringRes = R.string.bluetooth_pairing;//when profile is connected, information would be availableif (profileConnected) {// Update Meta data for connected deviceif (BluetoothUtils.getBooleanMetaData(mDevice, BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)) {leftBattery = BluetoothUtils.getIntMetaData(mDevice,BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY);rightBattery = BluetoothUtils.getIntMetaData(mDevice,BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY);}......
}/*** Get battery level from remote device* @return battery level in percentage [0-100], or {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN}*/public int getBatteryLevel() {//android.bluetooth.BluetoothDevice mDevice;return mDevice.getBatteryLevel();}
通过跟踪发现控制蓝牙图标显示代码在 PhoneStatusBarPolicy.java 中,只有当蓝牙设备连接成功后蓝牙图标才会显示,
默认用的资源文件为 stat_sys_data_bluetooth_connected.xml,我们就可以在这块做调整,当蓝牙设备连接成功后读取电量值,
根据电量值获取对应电池格数资源文件就ok了。
vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
@Overridepublic void onBluetoothStateChange(boolean enabled) {updateBluetooth();}private final void updateBluetooth() {//int iconId = R.drawable.stat_sys_data_bluetooth_connected;int iconId = R.drawable.ic_bluetooth_connected_super;//cczheng changeString contentDescription =mContext.getString(R.string.accessibility_quick_settings_bluetooth_on);boolean bluetoothVisible = false;if (mBluetooth != null) {if (mBluetooth.isBluetoothConnected()) {contentDescription = mContext.getString(R.string.accessibility_bluetooth_connected);bluetoothVisible = mBluetooth.isBluetoothEnabled();}}//20200602 cczheng addif (bluetoothVisible) {int battery = getBluetoothDeviceBattery();if (battery > 0) {iconId = getBatteryLevelIconId(battery);}}//EmIconController.setIcon(mSlotBluetooth, iconId, contentDescription);mIconController.setIconVisibility(mSlotBluetooth, bluetoothVisible);}//20200602 cczheng add for show bt headset battery Sprivate int getBatteryLevelIconId(int battery){int iconId = 0;if (battery <= 10) {iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_0;}else if (battery <= 20) {iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_1;}else if (battery <= 40) {iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_2;}else if (battery <= 60) {iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_3;}else if (battery <= 80) {iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_4;}else if (battery <= 100) {iconId = R.drawable.stat_sys_data_bluetooth_connected_battery_5;}Log.e(TAG, "iconId="+iconId);return iconId;}private int getBluetoothDeviceBattery(){int battery = -1;BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;try {Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);method.setAccessible(true);int state = (int) method.invoke(btAdapter, (Object[]) null);if (state == BluetoothAdapter.STATE_CONNECTED) {Set<BluetoothDevice> devices = btAdapter.getBondedDevices();for (BluetoothDevice device : devices) {Method batteryMethod = BluetoothDevice.class.getDeclaredMethod("getBatteryLevel", (Class[]) null);batteryMethod.setAccessible(true);Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);isConnectedMethod.setAccessible(true);boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);int level = (int) batteryMethod.invoke(device, (Object[]) null);if (device != null && level > 0 && isConnected) {String deviceName = device .getName();battery = level;android.util.Log.i(TAG, "Connected Bluetooth="+deviceName + " battery="+level);}}} else {android.util.Log.e(TAG, "No Connected Bluetooth Devices Found");}} catch (Exception e) {e.printStackTrace();}finally{return battery;}}//20200602 cczheng add for show bt headset battery Eprivate final void updateTTY() {TelecomManager telecomManager =(TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);if (telecomManager == null) {updateTTY(TelecomManager.TTY_MODE_OFF);} else {updateTTY(telecomManager.getCurrentTtyMode());}}
参考文章
Android 10 获取已连接的蓝牙设备的当前电量
Android7.0 获取蓝牙设备电量
androidQ(10.0) 读取蓝牙设备当前电量并显示相关推荐
- 安卓10 linux内核,AndroidQ(10.0) 内核版本增加linux编译用户信息
版本对比 O 版本设置界面中内核信息 Q 版本设置界面中内核信息 解决办法 frameworks\base\packages\SettingsLib\src\com\android\settingsl ...
- AndroidQ(10.0) 手机锁屏炫酷充电动画————html方案
效果图 知识储备 1.WebView加载html,并通过JS传值 在网上随便搜索找到了这个炫酷的充电动画,可惜是css实现的,想在Android中使用那只能 通过 WebView 来加载了,要传递当前 ...
- AndroidQ(10.0) MTK平台添加新分区
65 平台增加一个新分区,用于保存特殊数据,修改文件列表如下 modified: build/make/core/Makefilemodified: build/make/core/config.mk ...
- AndroidQ(10.0) 预制客供壁纸和铃声
预制客供壁纸 之前这个功能在 6.0 上修改过,已经是三年前了,之前壁纸选择还集成在 Launcher3 中,当时参考这篇 https://blog.csdn.net/jspping/article/ ...
- AndroidQ(10.0) 手机锁屏炫酷充电动画————lottie方案
效果图 知识储备 1.Lottie动画使用指南 Lottie开源动画库介绍与使用示例 Android Lottie动画初探 Lottie动画免费下载网站 2.SystemUI 中引入AAR库编译 因为 ...
- AndroidQ(10.0) Settings DatePick bug修改
问题现象 上图展示的很清楚,在系统->日期和时间选项页面中,点击日期 Preference,左边的上一月显示都透出来了 修改历程 一开始觉得是 DatePick 系统控件出问题了吧,自己新建了a ...
- [RK3288][Android6.0] 调试笔记 --- 电池电量一直显示100%
Platform: Rockchip OS: Android 6.0 Kernel: 3.10.92 之前文章[RK3288][Android6.0] 调试笔记 - 伪电池驱动添加 阐述了如何添加一个 ...
- Android 10.0锁屏界面默认不显示Notification通知
在系统开机以后,默认在锁屏界面如果有通知会显示的,但是这样客户觉得非常不方便,要求去掉显示的所有通知,为了满足客户需求 所以就要实现这个功能 在StatusBarNotificationPresent ...
- android 10.0 日历ActionBar图标不能正确显示日期的修改
在系统日历源码中,有个bug 就是头部显示日期是一个图标,所以客户提出是有问题的.结果看源码才发现是图片写死了的原因 所以接下来分析下修改 日历主页面AllInOneActivity.java 代码分 ...
最新文章
- Python基础之逻辑运算符
- mysql 主键 uniqo_项目总结,彻底掌握NodeJS中如何使用Sequelize
- 贪吃蛇小游戏源码再回顾
- 根本不值得一提的乒乓球国手王浩
- java JDBC入门及案例演示
- 关于C++中的继承感悟
- c语言——高精度除法
- matlab心电信号处理,基于MATLAB的心电信号的数字滤波处理
- WIN10桌面图标消失,且右键失效
- 如何启用计算机的无线功能键在哪,笔记本怎么打开wifi_如何开启笔记本电脑上的WiFi开关-win7之家...
- opencv Library QUIRC is not linked解决办法
- SCI投稿如何选择目标期刊
- Location is not available,the folder or directory is corrupted and unreadable
- 跳一跳,python脚本原理
- 数据库基础(常见面试题)
- VS2015程序工程的ICO图标研究
- ubuntu内核和驱动版本不兼容_彻底解决ubuntu循环登录和显卡驱动问题
- Fc2显示服务器拥挤,fc2视频全攻略
- JavaScript表单验证及注册界面
- Maya2018 快速绑定人物骨骼制作动画并导出fbx
热门文章
- nn.CrossEntropyLoss总结
- ios浏览器微信支付回调页面_iOS集成H5微信支付实现跳转与回调的解决方案
- 彻底搞懂0-1背包问题(动态规划)
- NGS项目四:高通量测序在植物生物胁迫研究中的应用
- 架构道术-时间的力量有多大
- 论文翻译:《Phosvardeep:使用序列信息对磷酸变化的深度学习预测》
- GaRy-Liang的linux成长日记3-自动化安装
- 好玩的python3代码_python好玩的项目—色情图片识别代码分享
- 2021-9.15基于目标速度的汽车 ACC系统油门控制策略研究-童宝锋
- 想要出国读博作博后的看过来:德国马普育种所植物与微生物互作方向招收3名博士1名博后