在Android设备与蓝牙设备进行数据交换时,需要实时监听蓝牙关闭和打开的状态、蓝牙与设备的连接状态,
进而对程序进行相应的逻辑处理,提高用户体验。本文实现了监听蓝牙的关闭和打开状态、蓝牙与设备的连接
状态,详细的实现代码如下:

注意:监听蓝牙状态时,请使用系统全局广播,本地广播无法实现蓝牙状态的监听

1、安卓权限

<uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

2、XML代码(activity_bluetooth_monitor.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BluetoothMonitorActivity"
    android:orientation="vertical">

<Button
        android:id="@+id/open_ble"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开蓝牙"/>

<Button
        android:id="@+id/close_ble"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="关闭蓝牙"/>

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="蓝牙设备的连接状态,可以使用两部手机进行试验,先对两部手机进行配对,然后再
进行手动连接,就会显示连接状态!"/>
</LinearLayout>

3、Activity代码

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class BluetoothMonitorActivity extends AppCompatActivity {

private Button openBle = null;
    private Button closeBle = null;
    private BluetoothMonitorReceiver bleListenerReceiver = null;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_monitor);

// 初始化广播
        this.bleListenerReceiver = new BluetoothMonitorReceiver();
        IntentFilter intentFilter = new IntentFilter();
        // 监视蓝牙关闭和打开的状态
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

// 监视蓝牙设备与APP连接的状态
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);

// 注册广播
        registerReceiver(this.bleListenerReceiver, intentFilter);

// 初始化控件
        this.closeBle = findViewById(R.id.close_ble);
        this.openBle = findViewById(R.id.open_ble);

// 关闭蓝牙
        this.closeBle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

/*
                // 跳转到蓝牙设置页面,关闭蓝牙,没有发现弹出对话框关闭蓝牙的
                Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
                startActivityForResult(intent,RESULT_OK);
                */

// 隐式关闭蓝牙
                BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                bluetoothAdapter.disable();

}
        });

// 打开蓝牙
        this.openBle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

/*
                // 弹出对话框,打开蓝牙
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(intent,RESULT_OK);
                */

// 隐式打开蓝牙
                BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                bluetoothAdapter.enable();

}
        });
    }

@Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(this.bleListenerReceiver);
    }
}

4、广播代码

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BluetoothMonitorReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action != null){
            switch (action) {
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    switch (blueState) {
                        case BluetoothAdapter.STATE_TURNING_ON:
                            Toast.makeText(context,"蓝牙正在打开",Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothAdapter.STATE_ON:
                            Toast.makeText(context,"蓝牙已经打开",Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothAdapter.STATE_TURNING_OFF:
                            Toast.makeText(context,"蓝牙正在关闭",Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothAdapter.STATE_OFF:
                            Toast.makeText(context,"蓝牙已经关闭",Toast.LENGTH_SHORT).show();
                            break;
                    }
                    break;

case BluetoothDevice.ACTION_ACL_CONNECTED:
                    Toast.makeText(context,"蓝牙设备已连接",Toast.LENGTH_SHORT).show();
                    break;

case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    Toast.makeText(context,"蓝牙设备已断开",Toast.LENGTH_SHORT).show();
                    break;
            }

}
    }
}

Android监听蓝牙与设备连接状态、关闭和打开状态相关推荐

  1. android hdmi 监听,对于HDMI设备连接状态的监听

    对与最近主要做的是电视机盒子端的开发,其中涉及到设备的状态监听比较繁琐,所以对HDMI的连接状态的监听方法做个记录,方便后续查看. 主要通过两种方式: (1)比较常用的广播监听 注册一个动态广播来获取 ...

  2. Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理

    Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理 目录 Android Studi ...

  3. Android通过广播接收器BroadcastReceiver监听蓝牙连接变化

    上一篇介绍了通过广播对蓝牙的打开关闭等变化的监听,这一篇将对蓝牙连接状态的变化进行封装. 首先是广播接收器BlueToothConnectReceiver 类 import android.bluet ...

  4. android 蓝牙相关广播,Android通过广播接收器BroadcastReceiver监听蓝牙连接变化

    上一篇介绍了通过广播对蓝牙的打开关闭等变化的监听,这一篇将对蓝牙连接状态的变化进行封装. 首先是广播接收器BlueToothConnectReceiver 类 import android.bluet ...

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

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

  6. android 监听网络状态

    今天,讲讲怎么监听手机网络状态的改变. 一.加入网络权限 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:nam ...

  7. android 10.0 监听Soft Ap热点连接与断开

    1.概述 在10.0的系统产品开发中,在定制化银行设备时,需要设备开机后打开热点,然后监听顾客连接设备和断开设备 那么SoftApManager.java 就主要负责热点连接的管理,就需要分析它的相关 ...

  8. Android 监听U盘OTG挂载状态

    Android 监听U盘OTG挂载状态 本篇博客介绍下在Android系统中,如何监听和获取U盘OTG挂载状态 在Android中插入U盘时,系统会先准备U盘并检查是否有错误,检查完成后才会把U盘挂载 ...

  9. 虾扯蓝牙(一)获取蓝牙当前状态,监听蓝牙手动开关,代码开关蓝牙

    前言 半年没有更新博客了,首要原因就是我太懒了,其次是换了新工作,新环境,一直都处于适应与学习阶段,因为涉及到的领域以前都是没接触的–有幸外派到海尔做智能家居,第一次到这种大的公司来敲代码,很多东西都 ...

  10. Android 监听 WiFi 开关状态

    Android 监听 WiFi 开关状态 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/70854309 本文出自[赵彦军的博客] ...

最新文章

  1. 开发日记-20190510
  2. caffe教程翻译:Alex’s CIFAR-10 tutorial, Caffe style
  3. 并行数据库 分布式数据库
  4. 是前端类库还是前端框架?
  5. Hadoop学习之整体介绍及环境搭建
  6. 解决centos下sudo运行tshark,写入文件显示:Permission denied.
  7. 新浪微博客户端(42)-完善表情键盘的工具条
  8. python3生成文件对象(五分钟读懂)
  9. Java split 分割 字符串(分隔符如:* ^ : | , .) 及注意点 要转义
  10. 三星电子首次公布李在镕薪酬 看起来一点儿也不高
  11. 短链接生成接口、长链接转换短链接,可根据ip归属地个性化跳转、随机跳转
  12. 《麦肯锡方法》第10章 演示汇报-思维导图
  13. 福利卡巴斯基一年免费以及一些使用软件
  14. unix bsd linux shell bash GNU之间的联系,歪讲Linux(一)
  15. 学了这么久的编程,编程语言创始人你知道几个?
  16. 电脑维修小知识(我抄的!)
  17. 3CDaemon使用教程
  18. Spring Cloud Stream报错:Invalid bean definition with name:bean definition with this name already exist
  19. 用JAVA 做一个简易版的坦克大战(只实现基本功能)
  20. KITTI车辆检测数据集转VOC格式(亲测成功,附KITTI云盘连接以及完整格式转换代码)- KITTI车辆检测数据集看着一篇就够了!

热门文章

  1. 苹果修改wifi密码登陆服务器密码,iphone手机修改wifi密码
  2. 三菱FX3UFX2NFX1N PLC 模拟器模拟通信功能,模拟PLC实体,FX3U仿真器,仿真PLC服务器
  3. 完整缓和曲线和非完整缓和曲线交点法坐标计算应用
  4. BZOJ 1260涂色 paint
  5. 基于ZYNQ的帧差法多运动目标检测(开源)
  6. c语言例题15:折半查找
  7. C#生成与识别条形码、二维码示例 zxing、 barcodelib生成条形码 code128B等 无白边 自动宽度
  8. 什么是栈?栈的特点和应用场景
  9. 【超全面】Python内置函数详解
  10. Windows10自带应用的卸载和恢复