浅谈Bluetooth蓝牙开发

简介: 前言:项目用到蓝牙开发,具体蓝牙获取硬件传感器中的数据。 因为没有蓝牙开发的相关经验,决定先了解一些蓝牙开发的知识,再去看之前同事写的蓝牙相关代码。   --------------------------------------------------------------------------------------------------- 一、蓝牙开发相关类 1、BluetoothAdapter  显而易见,蓝牙适配器。

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

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

 

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

一、蓝牙开发相关类

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文件中添加权限,这个是必须要的

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

 

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<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项目实战(二十五):蓝牙连接硬件设备开发规范流程

相关文章
|
6月前
|
传感器 移动开发 物联网
【Bluetooth开发】蓝牙开发入门
【Bluetooth开发】蓝牙开发入门
71 0
|
7月前
|
Linux
kali2022.1蓝牙(bluetooth)
kali2022.1蓝牙(bluetooth)
253 2
|
Java 开发工具 Android开发
Android 蓝牙开发,蓝牙连打印机。(2)
Android 蓝牙开发,蓝牙连打印机。(2)
594 0
|
6月前
|
物联网 人机交互 数据格式
[资料]蓝牙开发
[资料]蓝牙开发
|
8月前
|
传感器 物联网 Android开发
Android蓝牙使用详解(低功耗蓝牙(BLE))
Android蓝牙使用详解(低功耗蓝牙(BLE))
|
Java Android开发 开发工具
Android 蓝牙开发,蓝牙连打印机。
Android 蓝牙开发,蓝牙连打印机。
234 0
Android 蓝牙开发,蓝牙连打印机。
|
Java Android开发
|
物联网 Android开发
蓝牙BLE(BlueTooth BLE)入门及爬坑指南
前言 最近比较忙,两三周没有更新简书了,公司正好在做蓝牙BLE的项目,本来觉得挺简单的东西从网上找了个框架,就咔咔地开始搞,搞完以后才发现里面还有不少坑呢,故而写一篇蓝牙BLE入门及爬坑指南,旨在帮助刚入蓝牙BLE的小伙伴们少走弯路。
2699 0
|
iOS开发
iOS Bluetooth(蓝牙)
1. 蓝牙发送照片 #import "ViewController.h" #import @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView ...
944 0