首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >浅谈Bluetooth蓝牙开发

浅谈Bluetooth蓝牙开发

作者头像
听着music睡
发布2018-05-18 15:52:30
1.4K0
发布2018-05-18 15:52:30
举报
文章被收录于专栏:Android干货Android干货

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

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

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

一、蓝牙开发相关类

1、BluetoothAdapter

 显而易见,蓝牙适配器。

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

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

2、BluetoothDevice

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

获取方法:

BluetoothData.SENSOR_DOWN_ADRESS = “20:16:05:25:32:31”; //MAC地址

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

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

1、权限

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

    //在程序中使用蓝牙功能
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    //启动设备发现或操纵蓝牙设置
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

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

BluetoothAdapter mBluetoothAdapter =  BluetoothAdapter.getDefaultAdapter();

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<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        //如果有配对的设备
        if(pairedDevices.size() > 0){
            ArrayList<String> 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);

相关知识:

Android项目实战(二十五):蓝牙连接硬件设备开发规范流程

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-09-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档