博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浅谈Bluetooth蓝牙开发
阅读量:7058 次
发布时间:2019-06-28

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

前言:项目用到蓝牙开发,具体蓝牙获取硬件传感器中的数据。

因为没有蓝牙开发的相关经验,决定先了解一些蓝牙开发的知识,再去看之前同事写的蓝牙相关代码。

 

---------------------------------------------------------------------------------------------------

一、蓝牙开发相关类

1、BluetoothAdapter

 显而易见,蓝牙适配器。

 通过这个类我们进行以下操作:

1、开关蓝牙设备2、扫描蓝牙设备3、设置/获取蓝牙状态信息,例如:蓝牙状态值、蓝牙Name、蓝牙Mac地址等;

 

2、BluetoothDevice

蓝牙设备,是我们连接的设备

获取方法:

BluetoothData.SENSOR_DOWN_ADRESS = “20:16:05:25:32:31”; //MAC地址
BluetoothDevice sensor_down = mBluetoothAdapter.getRemoteDevice(BluetoothData.SENSOR_DOWN_ADRESS);

 

 

---------------------------------------------------------------------------------------------------

二、蓝牙开发使用的基本步骤

1、权限

这个在AndroidManifest.xml文件中添加权限,这个是必须要的

//在程序中使用蓝牙功能    
//启动设备发现或操纵蓝牙设置

 

2、获得蓝牙适配器 BluetoothAdapter 对象 并根据获得结果判断当前设备是否支持蓝牙

BluetoothAdapter mBluetoothAdapter =  BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null){
//设备不支持蓝牙功能 Toast.makeText(this,"当前设备不支持蓝牙功能",Toast.LENGTH_SHORT).show(); return ; }

 

3、在设备支持蓝牙功能的情况下,我们需要判断蓝牙功能是否开启,若没开启,需给之开启

isEnabled()判断是否打开蓝牙功能,enable()方法用于打开蓝牙功能

if(!mBluetoothAdapter.isEnabled()){            boolean enable = mBluetoothAdapter.enable(); //返回值表示 是否成功打开了蓝牙功能            if(enable){                Toast.makeText(this,"打开蓝牙功能成功!",Toast.LENGTH_SHORT).show();            }else{                Toast.makeText(this,"打开蓝牙功能失败,请到'系统设置'中手动开启蓝牙功能!",Toast.LENGTH_SHORT).show();                return ;            }        }

 

4、查询已经配对的蓝牙设备

Set
pairedDevices = mBluetoothAdapter.getBondedDevices(); //如果有配对的设备 if(pairedDevices.size() > 0){ ArrayList
mArrayAdapter = new ArrayList<>(); for(BluetoothDevice device : pairedDevices){ //通过array adapter在列表中添加设备名称和地址 mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); Log.i("bluetooth",device.getName() + "\n" + device.getAddress()); } }else{ Toast.makeText(this,"暂无已配对设备",Toast.LENGTH_SHORT).show(); }

 

5、查询附近可用的蓝牙设备

方法很简单,就一句话

 

mBluetoothAdapter.startDiscovery();

注意此方法是异步执行的,相当于开启一个子线程,整个过程大约耗时12秒。

切记,当我们搜索并成功连接到我们需要的设备的时候,需要及时的关闭搜索行为,可以使用cancelDiscovery。

 

接下来我们需要写一个广播来接收查询到的设备数据

private final BroadcastReceiver mReceiver = new BroadcastReceiver(){        public void onReceive(Context context, Intent intent){            String action = intent.getAction();            allBlueToothDevice = new ArrayList<>();            if(BluetoothDevice.ACTION_FOUND.equals(action)){                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);                Log.i("bluetooth","可配对设备:"+device.getName() + "\n" + device.getAddress());            }        }    };

记得在onCreate()方法中注册广播:

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter);

 

 

相关知识:

转载地址:http://pggol.baihongyu.com/

你可能感兴趣的文章
php中奖概率算法,可用于刮刮卡,大转盘等抽奖算法
查看>>
go 学习资源和GitHub库
查看>>
Zabbix触发器函数(取前后差值)
查看>>
Postman
查看>>
STS开发环境搭建与配置
查看>>
moment.js aka underscore.date.js
查看>>
精悍的Python代码段
查看>>
逻辑卷管理
查看>>
16Mybatis_动态sql_if判断
查看>>
部署.NET开发环境
查看>>
Android 使用Android Studio + Gradle 或 命令行 进行apk签名打包
查看>>
JIT IR,C2
查看>>
构造函数
查看>>
[.NET] RabbitMQ 的行为艺术
查看>>
Swift 类型嵌套
查看>>
K线图
查看>>
软件工程---gjb438b 质量规范体系
查看>>
C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 更优美的Oracle数据库上的代码生成器...
查看>>
一张图读懂阿里云产品全新价格调整
查看>>
四十、【GridView】
查看>>