Android - 接收蓝牙状态改变的广播

Android - 接收蓝牙状态改变的广播,第1张

概述文章目录准备接收蓝牙状态改变的广播参考YourapplicationcanlistenfortheACTION_STATE_CHANGEDbroadcastintent,whichthesystembroadcastswhenevertheBluetoothstatechanges.ThisbroadcastcontainstheextrafieldsEXTRA_STATEandEXTRA_PRE

文章目录准备接收蓝牙状态改变的广播参考
Your application can Listen for the ACTION_STATE_CHANGED broadcast intent, which the system broadcasts whenever the Bluetooth state changes. This broadcast contains the extra fIElds EXTRA_STATE and EXTRA_PREVIOUS_STATE, 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 行):

<?xml version="1.0" enCoding="utf-8"?><manifest ...>    <!-- Allows applications to connect to paired bluetooth devices. -->    <uses-permission androID:name="androID.permission.BLUetoOTH" /></manifest>

编辑 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();            }        }    };}
参考

Broadcasts - Overview - Receiving broadcasts

Bluetooth - Overview - Set up bluetooth

Manifest.permission.BLUETOOTH

BluetoothAdapter.ACTION_STATE_CHANGED

总结

以上是内存溢出为你收集整理的Android - 接收蓝牙状态改变的广播全部内容,希望文章能够帮你解决Android - 接收蓝牙状态改变的广播所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1041244.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存