蓝牙4.0开发及步骤

蓝牙4.0相关类的探索:

一 、 BluetoothAdapter 介绍:

代表了移动设备的本地的蓝牙适配器 , 通过该蓝牙适配器可以对蓝牙进行基本操作, 例如 : 启动设备发现(startDiscovery), 获取已配对设备(getBoundedDevices), 通过mac蓝牙地址获取蓝牙设备(getRemoteDevice), 从其它设备创建一个监听连接(listenUsingRfcommWithServiceRecord);

BluetoothAdapter.getDefaultAdapter()该静态方法可以获取该适配器对象.

常用的几个方法:

cancelDiscovery() 根据字面意思,是取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索

disable()关闭蓝牙

enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:

Intemtenabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enabler,reCode);//同startActivity(enabler);

getAddress()获取本地蓝牙地址

getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter

getName()获取本地蓝牙名称

getRemoteDevice(String address)根据蓝牙地址获取远程蓝牙设备

getBondedDevices();保护蓝牙设备

getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)

isDiscovering()判断当前是否正在查找设备,是返回true

isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false

listenUsingRfcommWithServiceRecord(String name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步

startDiscovery()开始搜索,这是搜索的第一步

LeScanCallback() BLE(蓝牙低能耗技术)扫描回调接口,发现BLE设备

startLeScan()开始扫描,需添加LeScanCallback()回调对象

stopLeScan()停止扫描,需添加LeScanCallback()回调对象

二、 BluetoothDevice 介绍:

顾名思义该类代表来蓝牙设备描述

createRfcommSocketToServiceRecord(UUIDuuid)根据UUID创建并返回一个BluetoothSocket
这个方法也是我们获取BluetoothDevice的目的——创建BluetoothSocket
这个类其他的方法,如getAddress(),getName(),同BluetoothAdapter

使用 BluetoothAdapter 用于创建 BluetoothDevice

通过GATT协议连接设备 BluetoothDevice.connectGatt(this, false, mGattCallback);
两个设备通过BLE通信,首先需要建立GATT连接。这里我们讲的是Android设备作为client端,连接GATT Server。
连接GATT Server,你需要调用BluetoothDevice的connectGatt()方法。此函数带三个参数:Context、autoConnect(boolean)和BluetoothGattCallback对象。调用示例:

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

函数成功,返回BluetoothGatt对象,它是GATT profile的封装。通过这个对象,我们就能进行GATT Client端的相关操作。BluetoothGattCallback用于传递一些连接状态及结果。

三、BluetoothGattCallback (抽象类)介绍:

public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {}连接状态改变

public void onServicesDiscovered(BluetoothGatt gatt, int status) {}发现服务

public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,int status) {}特征读取

public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {}特征写入

public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {}特征改变

public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,int status) {}描述读取

public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,int status) {}描述写入

public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {}写入完成回调

onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status){}获得RSSI(接收信号强度指示)

四、BluetoothServerSocket 介绍:

两个重载的accept(),accept(inttimeout)两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行!

还有一点需要注意的是,这两个方法都返回一个BluetoothSocket,最后的连接也是服务器端与客户端的两个BluetoothSocket的连接

close()这个就不用说了吧,翻译一下——关闭!

五、BluetoothSocket 介绍:

跟BluetoothServerSocket相对,是客户端

一共5个方法,不出意外,都会用到

  close(),关闭

  connect()连接

  getInptuStream()获取输入流

  getOutputStream()获取输出流

  getRemoteDevice()获取远程设备,这里指的是获取bluetoothSocket指定连接的那个远程蓝牙设备

六、BluetoothManager 介绍:

蓝牙管理器可以获得BluetoothAdapter

mBluetoothManager = (BluetoothManager) getSystemServic(Context.BLUETOOTH_SERVICE);

mBluetoothAdapter = mBluetoothManager.getAdapter();

七、BluetoothGatt 介绍:

此类提供GATT(协议)蓝牙功能,以使与蓝牙智能或智能设备的通信。

BluetoothGatt常规用到的几个操作示例:

connect() :连接远程设备。
discoverServices() : 搜索连接设备所支持的service。
disconnect():断开与远程设备的GATT连接。
close():关闭GATT Client端。
readCharacteristic(characteristic) :读取指定的characteristic。
setCharacteristicNotification(characteristic, enabled) :设置当指定characteristic值变化时,发出通知。
getServices() :获取远程设备所支持的services。

八、总结:

一个中央可以同时连接多个周边,但是一个周边某一时刻只能连接一个中央。

大概了解了概念后,看看Android BLE SDK的四个关键类(class):

a) BluetoothGattServer作为周边来提供数据;BluetoothGattServerCallback返回周边的状态。

b) BluetoothGatt作为中央来使用和处理数据;BluetoothGattCallback返回中央的状态和周边提供的数据。

c)每一个周边BluetoothGattServer,包含多个服务Service,每一个Service包含多个特征Characteristic。

image

1.先拿到BluetoothManager:bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

2.再拿到BluetoothAdapt:btAdapter = bluetoothManager.getAdapter();

3.开始扫描:btAdapter.startLeScan( BluetoothAdapter.LeScanCallback);

4.从LeScanCallback中得到BluetoothDevice:public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {…..}

5.用BluetoothDevice得到BluetoothGatt:gatt = device.connectGatt(this, true, gattCallback);

终于拿到中央BluetoothGatt了,它有一堆方法(查API吧),调用这些方法,你就可以通过BluetoothGattCallback和周边BluetoothGattServer交互了。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值