ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-02-27 09:57:17  阅读:311  来源: 互联网

标签:OFF BluetoothAdapter 蓝牙 广播 break STATE import TURNING Android


文章目录


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.1
Build #AI-201.8743.12.41.6953283, built on November 5, 2020
Runtime version: 1.8.0_242-release-1644-b01 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 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

标签:OFF,BluetoothAdapter,蓝牙,广播,break,STATE,import,TURNING,Android
来源: https://blog.csdn.net/qq_29761395/article/details/114161946

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有