Android - 接收蓝牙状态改变的广播
发布日期:2021-05-06 23:02:48 浏览次数:20 分类:精选文章

本文共 3908 字,大约阅读时间需要 13 分钟。

文章目录

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
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();            }        }    };}

参考

上一篇:Android - 自定义按钮(Button)点击、获取焦点的背景
下一篇:Android - Access app-specific files(不完整)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月14日 11时06分13秒