Android 10 获取已连接上的蓝牙设备的当前电量
前言
最近的项目中有获取连接蓝牙设备电量的需求,查找了一些资料,发现谷歌在Android8.0推出了一个getBatteryLevel的api,用来获取蓝牙设备电量百分比的方法,但在我的项目中android10环境,这个方法在Bluetoothdevice源码内,被标识为废弃不可直接调用的方法。如下图所示

但是研究一番发现可以通过反射,继续调用这个方法。
下面一行就是核心代码啦,level就是当前蓝牙电量的百分比
int level = (int) batteryMethod.invoke(device, (Object[]) null);//level就是当前蓝牙电量百分比
我将详细过程写入了一个工具类内,可以看到其实也非常的简单。
下面的代码仅为给各位同学提供一个思路,可以直接拿来使用,希望能帮到有需要的同学~
工具类代码
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import java.lang.reflect.Method;
import java.util.Set;
/**
* @description: 蓝牙方法工具类
* @author: ODM
* @date: 2020/4/13
*/
public class BluetoothUtils {
/**
* 获取已连接的蓝牙设备的电量
*/
public static void getBluetoothDeviceBattery(){
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
//获取BluetoothAdapter的Class对象
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();
LogUtils.d(deviceName + " 电量: " + level);
}
}
} else {
ToastUtils.showLong("No Connected Bluetooth Devices Found");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Android 10 获取已连接上的蓝牙设备的当前电量的更多相关文章
- SuperSocket获取所有连接上的 Session
你也可以从 AppServer 实例获取所有连接上的 session 然后推送数据到所有客户端: foreach(var session in appServer.GetAllSessions()) ...
- Android 得到当前已连接的wifi的信号强度
1.得到当前已连接的wifi信息 WifiManager wifi_service = (WifiManager)getSystemService(WIFI_SERVICE); WifiInfo wi ...
- Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityManager 类,用 ...
- Android编程 获取网络连接状态 及调用网络配置界面
获取网络连接状态 随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityMan ...
- Android编程获取网络连接状态及调用网络配置界面
获取网络连接状态 随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityMan ...
- iOS 获取已连接的wifi信息
转:http://blog.csdn.net/marujunyy/article/details/16843173 首先需要 #import <SystemConfiguration/Cap ...
- win7右下角无线网图标显示未连接,但是实际上已连接上,也能上网
首先,要确实是不是服务启动的问题,方法很简单,重新启动电脑就可以. 如果问题依旧,那么按下Win+R快捷键,输入“services.msc”,打开服务界面. 然后会看到右侧窗口出现好多设置项,找到“R ...
- RxHttp 完美适配Android 10/11 上传/下载/进度监听
1.前言 随着Android 11的正式发布,适配Android 10/11 分区存储就更加的迫切了,因为Android 11开始,将强制开启分区存储,我们就无法再以绝对路径的方式去读写非沙盒目录下的 ...
- android上使用蓝牙设备进行语音输入
主要实现步骤如下:1.确保已经和蓝牙耳机配对连接上.2.开启蓝牙信道AudioManager mAudioManager = (AudioManager)getSystemService(Contex ...
随机推荐
- Linq 学习——将List集合作为筛选条件查询数据
例: A表是一个List集合,B表也是一个List集合 .A与B有一个共同的字段 RecognitionCarCode B表通过RecognitionCarCode去重后拿到两个值{'1','2'}记 ...
- 【高并发】不废话,言简意赅介绍BlockingQueue
写在前面 最近,有不少网友留言提问:在Java的并发编程中,有个BlockingQueue,它是个阻塞队列,为何要在并发编程里使用BlockingQueue呢?好吧,今天,就临时说一下Blocking ...
- 表字段或表名出现Mysql关键字或保留字导致问题 Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have
MySQL 5.7使用的关键字和保留字 https://dev.mysql.com/doc/refman/5.7/en/keywords.html 当我们建表的时候如果使用了关键字或者保留字,则在执行 ...
- stand up meeting 12/21/2015
part 组员 工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 完成PDF UI主页面的页面切换功能,待完善 4 完善 ...
- Gitflow分支管理策略
Gitflow存在两个记录项目历史的分支 Master分支:存储(官方的,正式的)项目发布历史记录的分支. develop分支:充当功能的集成分支. Develop分支将包含项目的完整历史记录,而ma ...
- Git敏捷开发--stash命令
save 执行git stash,默认以commit info保存当前的stash信息 当在某个commit下,执行多次stash时,无法友好地区分每个stash的改动.save 命令可以清晰地标识每 ...
- 2.react-插件
PC: antd(蚂蚁金服)https://ant.design/index-cn 移动: mobile-antd(蚂蚁金服)https://mobile.ant.design =========== ...
- 这份Mybatis总结,我觉得你很需要!
前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y Mybatis应该是国内用得最多的「数据访问层」 ...
- 几个可以提高工作效率的Python内置小工具
在这篇文章里,我们将会介绍4个Python解释器自身提供的小工具.这些小工具在笔者的日常工作中经常用到,减少了各种时间的浪费,然而,却很容易被大家忽略.每当有新来的同事看到我这么使用时,都忍不住感叹, ...
- EasyPoi 导入导出Excel时使用GroupName的踩坑解决过程
一.开发功能介绍: 简单的一个excel导入功能 二.Excel导入模板(大致模板没写全): 姓名 性别 生日 客户分类 联系人姓名 联系人部门 备注 材料 综合 采购 张三 男 1994/05/25 ...