因项目需求的需要,需要实现android TV 机顶盒连接成功的蓝牙遥控器的低电量提醒。根据需求,要先获得已连接的蓝牙设备的电量,再根据电量的值做判断提醒用户。

下面直接看代码,本项目中一共有三个类,初始MainActivity,MyBroadcastReceiver,MyService.其中,MainActivity不做任何修改,MyBroadcastReceiver监听开机完成广播,启动服务MyService,读取电量以及提醒的功能全都在服务中实现。

MyBroadcastReceiver类:

public class MyBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Log.d("aaa" , "Boot Completeed .");Intent service = new Intent(context,MyService.class);context.startService(service);}
}

MyService类:

public class MyService extends Service {private static final boolean DEBUG = false;private static final String TAG = "aaa";public static Context context;BluetoothDevice mDevice;BluetoothGatt mBluetoothGatt;IntentFilter filter ;private static final UUID GATT_BATTERY_SERVICE_UUID =UUID.fromString("0000180f-0000-1000-8000-00805f9b34fb");private static final UUID GATT_BATTERY_LEVEL_CHARACTERISTIC_UUID =UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb");private static final String KEY_BATTERY = "battery";private Handler handler;@Overridepublic void onCreate() {Log.d("aaa" , "service created.");handler = new Handler();context = getApplicationContext();super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("aaa" , "service is running.");filter = new IntentFilter();filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);registerReceiver(broadcastReceiver,filter);return super.onStartCommand(intent,flags,startId);}BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if(intent.getAction().equals(BluetoothDevice.ACTION_ACL_CONNECTED)){mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);Log.d("aaa" , "Device's name is :"+mDevice.getName());mBluetoothGatt = mDevice.connectGatt(context,true,mGattCallback);}if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){Log.d("aaa" , "Device is disConected.");mBluetoothGatt.close();}}};private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {@Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {//Toast.makeText(MyService.this,"the pross is going to callback Fucktion",Toast.LENGTH_LONG).show();Log.d("aaa" , "the pross is going to callback Fucktion :");Log.d("aaa" , "status is :"+status);Log.d("aaa" , "newState is :"+newState);if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED) {gatt.discoverServices();}}@Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) {if (status != BluetoothGatt.GATT_SUCCESS) {if (DEBUG) {Log.e(TAG, "Service discovery failure on " + gatt);}return;}final BluetoothGattService battService = gatt.getService(GATT_BATTERY_SERVICE_UUID);if (battService == null) {if (DEBUG) {Log.d(TAG, "No battery service");}return;}final BluetoothGattCharacteristic battLevel =battService.getCharacteristic(GATT_BATTERY_LEVEL_CHARACTERISTIC_UUID);if (battLevel == null) {if (DEBUG) {Log.d(TAG, "No battery level");}return;}gatt.readCharacteristic(battLevel);}@Overridepublic void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {if (status != BluetoothGatt.GATT_SUCCESS) {if (DEBUG) {Log.e(TAG, "Read characteristic failure on " + gatt + " " + characteristic);}return;}if (GATT_BATTERY_LEVEL_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {final int batteryLevel =characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);Log.d(TAG, "battery level is:"+batteryLevel);handler.post(new Runnable() {@Overridepublic void run() {if(batteryLevel >90)Toast.makeText(context,"The remote control has enough power",Toast.LENGTH_LONG).show();if(batteryLevel <10)Toast.makeText(context,"The remote control is low in power, please replace the battery as soon as possible.",Toast.LENGTH_LONG).show();}});}}};@Overridepublic void onDestroy() {Toast.makeText(this , "service is destory.",Toast.LENGTH_LONG).show();unregisterReceiver(broadcastReceiver);Log.d("aaa" , "Device is onDestory.");super.onDestroy();}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}
}
manifest清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.mypeixun815"android:sharedUserId="android.uid.system"><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/><uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/><uses-feature android:name="android.hardware.bluetooth_le" android:required="true"></uses-feature><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><receiverandroid:name=".MyBroadcastReceiver"android:enabled="true"android:exported="true"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED"></action></intent-filter></receiver><serviceandroid:name=".MyService"android:enabled="true"android:exported="true" /><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

在android 10.0系统中运行没有问题,最后不要忘记获得系统签名以及赋予apk 可读可执行的权限。

 
 

andriod TV 获取已连接蓝牙遥控器电池电量总结相关推荐

  1. Android 监听网络连接状态,判断网络连接方式,9.0网络连接,获取已连接WiFi名称SSID和MAC

    获取已连接的WiFi名称 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> ...

  2. Windows 获取已连接WIFI密码

    获取当前已连接WIFI密码 1 打开控制面板,选择网络与共享中心. 2 点击当前连接的wifi: WLAN(图书馆 2F) . 3 点击无线属性(W) . 4 选择安全,然后勾选显示字符(H),就可以 ...

  3. Intel Realsense 如何获取已连接所有摄像头的序列号参数?context() query_devices() size() camera_info device_list

    import pyrealsense2 as rsctx = rs.context() devs = ctx.query_devices() # devs是device_list类 device_nu ...

  4. 程序员 破解获取已连接过的WiFi密码

    1.第一步:输入cmd,点击确定 2.第二步:输入命令(查看WiFi名字)netsh wlan show profiles 注(用户配置文件就是附近的WiFi名字) 3.第三步:输入命令netsh w ...

  5. android 获取手机蓝牙是否与其他设备蓝牙配对连接成功,android开发获取手机已连接的蓝牙设备(包括已链接的设备和已经配对绑定的设备)...

    在应用开发中有时会碰到操作蓝牙设备的需求,这时就需要获取手机已连接的蓝牙设备,其中"已连接"是广泛含义,其实蓝牙设备分为可链接设备(比如:蓝牙音箱 蓝牙耳机) 和可绑定设备(比如: ...

  6. 微信小程序自动连接蓝牙电子秤

    不同品牌的称,只需要打印下方函数返回值,根据自己需要做处理就可以了. wx.onBLECharacteristicValueChange(function (res) {let receiverTex ...

  7. android 获取可连接的wifi和wifi信号强度

    1.需要在AndroidManifest.xml中添加权限 <uses-permission android:name="android.permission.CHANGE_WIFI_ ...

  8. 如何查询计算机已连接wife的密码错误,三种方法查询已连接 WiFi 的密码,简单实用!...

    因为记性差,经常忘记各种密码,比如 WiFi 密码,当有朋友问到家里的无线网密码时,你就愣住了,俩人在那边干瞪眼.为了帮助大家解决这个尴尬问题,小编现在介绍三种方法,可以查询已经连接的 WiFi 的密 ...

  9. android获取已配对的蓝牙列表和已连接蓝牙设备

    private void getPairBLEAndConnectBLE() {BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaul ...

最新文章

  1. db2不记录日志插入记录
  2. Powershell记录2
  3. 设置背景图片模糊,内容不模糊
  4. Weblogic EJB 学习笔记(2)精
  5. docker安装rabbitmq延时队列插件
  6. python zope 工作流_使用C语言来扩展Python程序和Zope服务器的教程
  7. 百度图神经网络学习——day04:图神经网络算法(二)
  8. 现代操作系统-原理与实现(上)【银杏书-读书笔记】
  9. 卸载ie8的一些方法
  10. EfficientNetV2网络详解
  11. Oracle 19c遇到ORA-XXXX:????的问题的解决方案
  12. acg-faka--功能丰富的发卡二次元商城源码
  13. ICC 图文学习——LAB0:ICC图形界面
  14. Gauss消元法(特解与通解)
  15. ec20 以太网_整车通讯系统——车载以太网系统系列文章(第一篇)
  16. csp认证考试准备Day-3
  17. ambari mysql 密码_ambari密码重置
  18. 抓取猫眼电影实时数据
  19. 财务比率:ROE, 净利润增长率、毛利率、市盈率、PEG
  20. IBM z15揭开面纱,多项业界首创技术护航混合多云安全

热门文章

  1. 安全和运维工具脑图v1.0
  2. 【安全资讯】全球上市公司财报中“网络安全”的提及次数出现显著增长
  3. 乐理知识(和弦相关)
  4. jquery:关于$().css(left,)的设置;offset();offset().left();$.width();$.outerWidth();
  5. 经典伴读_GOF设计模式_结构型模式
  6. 用javascript根据当前年月获得当前季度
  7. texlive和texstudio安装及使用
  8. 一份风控模型性能提升秘籍奉上|附视频+实操(详版)
  9. Java、计算三角形的周长
  10. 32位/64位系统,jdk32位,64位,32/64位jdk编译出来的class和eclipse 32位和64位