
蓝牙通信二:测试手机蓝牙当前状态
发布日期:2021-05-09 18:26:52
浏览次数:19
分类:原创文章
本文共 1989 字,大约阅读时间需要 6 分钟。
import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothProfile;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.os.Bundle;import android.widget.TextView;import leon.android.chs_ydw_dcs480_dsp_408.R;/** * Created by Administrator on 2018\9\3 0003. */public class ControlPCActivity extends Activity { // isBlueCon的值 0表示未连接 1表示已经连接 2表示未打开 -1表示未知错误 //参考网址:https://blog.csdn.net/memoryjs/article/details/42968823 private BluetoothAdapter ba; //蓝牙适配器 int isBlueCon; TextView tv_contrcol; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contrcol); tv_contrcol = (TextView)findViewById(R.id.tv_contrcol); ba = BluetoothAdapter.getDefaultAdapter(); initView(); tv_contrcol.setText("蓝牙状态为:"+initView()); } private int initView() { //蓝牙适配器是否存在,即是否发生了错误 if (ba == null){ isBlueCon = -1; //error }else if(ba.isEnabled()){ int a2dp = ba.getProfileConnectionState(BluetoothProfile.A2DP); //可操控蓝牙设备,如带播放暂停功能的蓝牙耳机 int headset = ba.getProfileConnectionState(BluetoothProfile.HEADSET); //蓝牙头戴式耳机,支持语音输入输出 int health = ba.getProfileConnectionState(BluetoothProfile.HEALTH); //蓝牙穿戴式设备 //查看是否蓝牙是否连接到三种设备的一种,以此来判断是否处于连接状态还是打开并没有连接的状态 int flag = -1; if (a2dp == BluetoothProfile.STATE_CONNECTED) { flag = a2dp; } else if (headset == BluetoothProfile.STATE_CONNECTED) { flag = headset; } else if (health == BluetoothProfile.STATE_CONNECTED) { flag = health; } //说明连接上了三种设备的一种 if (flag != -1){ isBlueCon = 1; //discontinued } }else { isBlueCon = 2; //shut off } return isBlueCon; }}
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月20日 18时04分43秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
2年经验大专生,拿下阿里有多难?(已拿offer,附上面经)
2021-05-11
从需求出发:QMUI 最新版 QMUISchemeHandler 的设计与实现解析
2021-05-11
浅谈持续集成(CI)、持续交付(CD)、持续部署(CD)
2021-05-11
成为一个优秀的测试工程师需要具备哪些知识和经验?
2021-05-11
深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接
2021-05-11
HDU4814——数学,模拟进制转换
2021-05-11
LeetCode LCP 3 机器人大冒险
2021-05-11
Cisco网络模拟器踩坑记录
2021-05-11
LeetCode 947. 移除最多的同行或同列石头 并查集
2021-05-11
一些JavaSE学习过程中的思路整理(二)(主观性强,持续更新中...)
2021-05-11
JavaScript中如何给按钮设置隐藏与显示属性
2021-05-11
CSS——NO.4(继承、层叠、特殊性、重要性)
2021-05-11
Python——11面向对象编程基础
2021-05-11
Python——5函数
2021-05-11
C++中如何对单向链表操作
2021-05-11
C++走向远洋——63(项目二2、两个成员的类模板)
2021-05-11
6——PHP顺序结构&&字符串连接符
2021-05-11
C++扬帆远航——2
2021-05-11
C++扬帆远航——1
2021-05-11
上周热点回顾(5.3-5.9)
2021-05-11