
Android - 接收蓝牙状态改变的广播
Your application can listen for the broadcast intent, which the system broadcasts whenever the Bluetooth state changes. This broadcast contains the extra fields and , containing the new and old Bluetooth states, respectively. Possible values for these extra fields are
发布日期:2021-05-06 23:02:48
浏览次数:20
分类:精选文章
本文共 3908 字,大约阅读时间需要 13 分钟。
文章目录
STATE_TURNING_ON
, STATE_ON
, STATE_TURNING_OFF
, and STATE_OFF
. Listening for this broadcast can be useful if your app needs to detect runtime changes made to the Bluetooth state. 准备
IDE:
Android Studio 4.1.1Build #AI-201.8743.12.41.6953283, built on November 5, 2020Runtime version: 1.8.0_242-release-1644-b01 amd64VM: OpenJDK 64-Bit Server VM by JetBrains s.r.oWindows 10 10.0
接收蓝牙状态改变的广播
新建项目,选择 Empty Activity,在配置项目时,Minimum SDK
选择 API 16: Android 4.1 (Jelly Bean)
。
编辑 src\main\AndroidManifest.xml
应用清单文件,声明使用 android.permission.BLUETOOTH
权限(第 4 行):
编辑 MainActivity
文件:
package com.mk;import android.bluetooth.BluetoothAdapter;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); registerReceiver(broadcastReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(broadcastReceiver); } private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { String currentState = null; switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)) { case BluetoothAdapter.STATE_TURNING_ON: currentState = "BluetoothAdapter.STATE_TURNING_ON"; break; case BluetoothAdapter.STATE_ON: currentState = "BluetoothAdapter.STATE_ON"; break; case BluetoothAdapter.STATE_TURNING_OFF: currentState = "BluetoothAdapter.STATE_TURNING_OFF"; break; case BluetoothAdapter.STATE_OFF: currentState = "BluetoothAdapter.STATE_OFF"; break; default: currentState = "UNKNOWN"; break; } Toast.makeText(context, currentState, Toast.LENGTH_SHORT).show();// String previousState = null;// switch (intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, -1)) { // case BluetoothAdapter.STATE_TURNING_ON:// previousState = "BluetoothAdapter.STATE_TURNING_ON";// break;// case BluetoothAdapter.STATE_ON:// previousState = "BluetoothAdapter.STATE_ON";// break;// case BluetoothAdapter.STATE_TURNING_OFF:// previousState = "BluetoothAdapter.STATE_TURNING_OFF";// break;// case BluetoothAdapter.STATE_OFF:// previousState = "BluetoothAdapter.STATE_OFF";// break;// default:// previousState = "UNKNOWN";// break;// }//// Toast.makeText(context, previousState, Toast.LENGTH_SHORT).show(); } } };}
参考
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月14日 11时06分13秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
远程触发Jenkins的Pipeline任务的并发问题处理
2021-05-09
jackson学习之七:常用Field注解
2021-05-09
jackson学习之八:常用方法注解
2021-05-09
Web应用程序并发问题处理的一点小经验
2021-05-09
entity framework core在独立类库下执行迁移操作
2021-05-09
Asp.Net Core 2.1+的视图缓存(响应缓存)
2021-05-09
服务器开发- Asp.Net Core中的websocket,并封装一个简单的中间件
2021-05-09
没花一分钱的我竟然收到的JetBrains IDEA官方免费赠送一年的Licence
2021-05-09
Redis 集合统计(HyperLogLog)
2021-05-09
RE套路 - 关于pyinstaller打包文件的复原
2021-05-09
【wp】HWS计划2021硬件安全冬令营线上选拔赛
2021-05-09
Ef+T4模板实现代码快速生成器
2021-05-09
dll详解
2021-05-09
c++ static笔记
2021-05-09
C++中头文件相互包含与前置声明
2021-05-09
JQuery选择器
2021-05-09
MVC中在一个视图中,怎么加载另外一个视图?
2021-05-09
SQL--存储过程
2021-05-09
MVC学习系列5--Layout布局页和RenderSection的使用
2021-05-09
MVC学习系列13--验证系列之Remote Validation
2021-05-09