蓝牙BLE介绍

本文深入解析蓝牙BLE技术,涵盖蓝牙定义、版本变迁、BLE与传统蓝牙对比,详细解读BLE协议栈,包括控制器与主机部分,以及GATT架构,探讨BLE在物联网领域的应用。

蓝牙BLE介绍


本文主要包括以下内容:

1. 蓝牙定义及版本变化

2. BLE协议栈

3. GATT


1. 蓝牙定义及版本变化

1.1,蓝牙

蓝牙(英语:Bluetooth),一种无线通讯技术标准,用来让固定与移动设备,在短距离间交换数据,以形成个人局域网(PAN)。其使用短波特高頻(UHF)无线电波,经由2.4至2.485 GHz的ISM频段来进行通信。

1.2,蓝牙发布的版本状况大致如下:

蓝牙2.1

  • 使用最广,之前的很多产品都是这个版本,俗称经典蓝牙。

蓝牙3.0

  • 又名为高速蓝牙,在2.1的基础上大大提升了传输速度(24Mbps)。

蓝牙4.0

  • 最重要的特性是支持省电,即引入了低功耗蓝牙。

蓝牙4.1

  • 其目的是为了让 Bluetooth Smart 技术最终成为物联网(Internet of Things)发展的核心动力。

蓝牙5.0

  • 在2016年6月发布。在有效传输距离上将是4.2LE版本的4倍(理论上可达300米),传输速度将是4.2LE版本的2倍(速度上限为24Mbps)。蓝牙5.0还支持室内定位导航功能(结合WiFi可以实现精度小于1米的室内定位),允许无需配对接受信标的数据(比如广告、Beacon、位置信息等,传输率提高了8倍),针对物联网进行了很多底层优化。

1.3,蓝牙BLE与传统蓝牙的比较

BLE,全称为Bluetooth Low Energy,即蓝牙低功耗,较传统蓝牙最大的特点就是低功耗,多应用于对实时性要求较高,但对数据传输速率要求比较低的场景,比如血压计、键鼠等设备,而语音、音乐等对数据量传输比较大的场景依然需要使用传统蓝牙。

2. 协议栈

蓝牙BLE协议栈包含两部分:主机和控制器,如下图所示。

2.1,控制器部分

  • PHY:为Physical的缩写,负责数据的物理传输。
  • LL:为Link Layer的缩写,用于控制设备的射频状态。
  • HCI:为Host Controller interface的缩写,即主机和控制器之间的交互接口。

2.2,主机部分

  • L2CAP:为Logical Link Control and Adaption Protocol的缩写,负责管理逻辑层提供的逻辑链路。
  • SM:为Security Manager的缩写,为蓝牙配对和连接提供安全保证。
  • ATT:为Attributes的缩写,目的是允许设备向另外一个设备展示一块特定的数据,称之为“属性”。展示“属性”的设备称为服务器,与之配对的设备称为客户端。
  • GATT:为Generic Attributes的缩写,由一系列特征(characteristic)组成,这些每个特征又可能依赖不用的profiles。此profile后面会重点介绍。
  • GAP:为General Access Profile的缩写,主要描述了两个蓝牙设备建立通信时必要的基本操作,包括设备的发现、链路的建立和配置以及安全性设置。

2.3,BLE中的四个角色,按照通信之前是否需建立连接划分

不建立连接:

  • 广播者(Braodcaster):广播发送者,是不可连接的设备;
  • 观察者(Observer):扫描广播,不能够启动连接。

建立连接(通过GATT):

  • 中央(Central):扫描广播,启动连接,在单一或多链路层作为主机;
  • 外围(Peripheral):广播发送者,可连接的设备,在单一链路层作为从机。

3. GATT

GATT Profile架构图如下:

 

容易看出:

  • GATT Profile可由多个Service组成
  • 每个Service由多个Characteristic组成
  • 每个Characteristic由属性(Properties)、value 和 0至多个对此Characteristic的描述(Descriptor)所组成。在BLE设备连接建立成功之后读写,就是对Characteristic的读写。

另外,Service(多个)、Characteristic(多个)和 Descriptor(多个)使用UUID来作为各自的ID。

最后,附上官方对上图的说明:

The top level of the hierarchy is a profile, which is composed of one or more services necessary to fulfill a use case. 
A service is composed of characteristics or references to other services. 
A characteristic consists of a type (represented by a UUID), a value, a set of properties indicating the operations the characteristic supports and a set of permissions relating to security. 
It may also include one or more descriptors—metadata or configuration flags relating to the owning characteristic.

GATT groups these services to encapsulate the behavior of part of a device, and describes a use case, roles and general behaviors based on the GATT functionality. 
This framework defines procedures and formats of services and their characteristics, including discovering, reading, writing, notifying and indicating characteristics, as well as configuring the broadcast of characteristics.
GATT defines client and server roles. 

GATT procedures can be considered to be split into three basic types: 
Discovery procedures, Client-initiated procedures and Server-initiated procedures. 

The GATT server stores the data transported over the ATT and accepts ATT requests, commands and confirmations from the GATT client. 

The GATT server sends responses to requests and sends indications and notifications asynchronously to the GATT client when specified events occur on the GATT server. 
GATT also specifies the format of data contained on the GATT server.

参考如下:

  1. https://zh.wikipedia.org/wiki/%E8%97%8D%E7%89%99#%E8%97%8D%E7%89%99%E5%8D%94%E8%AD%B0%E5%A0%86%E7%96%8A
  2. http://m.elecfans.com/article/578020.html?from=bdhd_site
  3. http://m.elecfans.com/article/705867.html?from=bdhd_site
  4. http://www.mr-wu.cn/ten-important-differences-between-bluetooth-bredr-and-bluetooth-smart/
  5. 蓝牙官方文档
蓝牙BLE(Bluetooth Low Energy)是一种低功耗的无线通信技术,专为低功耗设备设计,适用于需要短距离通信且对能耗敏感的应用场景。BLE技术基于GATT(Generic Attribute Profile)协议栈,与传统蓝牙(经典蓝牙)相比,BLE在功耗、连接速度和使用场景上具有显著优势。 ### BLE技术原理 BLE的核心原理基于其协议栈架构,主要包括以下几层: 1. **物理层(PHY)**:负责定义无线通信的频率、调制方式等物理特性,BLE使用2.4GHz ISM频段,支持多种信道以减少干扰。 2. **链路层(LL)**:管理设备之间的连接状态,包括广播、扫描、连接建立等过程。 3. **主机控制接口(HCI)**:提供主机与控制器之间的通信接口。 4. **逻辑链路控制与适配协议(L2CAP)**:提供数据分片和重组功能。 5. **属性协议(ATT)**:定义数据存储和访问的方式,基于客户端-服务器模型。 6. **通用属性配置文件(GATT)**:基于ATT协议,定义了服务、特征值等概念,用于描述设备的功能[^1]。 ### BLE应用开发指南 在Android平台上进行BLE应用开发,主要涉及以下几个步骤: 1. **权限声明**:在应用的`AndroidManifest.xml`文件中声明必要的权限,例如: ```xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> ``` 其中最后一行用于声明应用仅适用于支持BLE的设备[^4]。 2. **初始化蓝牙适配器**:检查设备是否支持BLE,并初始化蓝牙适配器。 ```java BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter(); if (bluetoothAdapter == null || !getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { // 设备不支持BLE finish(); } ``` 3. **扫描BLE设备**:使用`BluetoothLeScanner`进行设备扫描。 ```java BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner(); ScanSettings settings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); scanner.startScan(Arrays.asList(new ScanFilter.Builder().build()), settings, scanCallback); ``` 4. **连接BLE设备**:找到目标设备后,调用`connectGatt()`方法进行连接。 ```java BluetoothGatt bluetoothGatt = device.connectGatt(this, false, gattCallback); ``` 5. **发现服务**:连接成功后,调用`discoverServices()`方法来获取设备的服务列表。 ```java @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { gatt.discoverServices(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { List<BluetoothGattService> servicesList = gatt.getServices(); // 处理服务列表 } ``` 6. **数据交互**:通过读写特征值实现数据交互。 ```java BluetoothGattService service = bluetoothGatt.getService(SERVICE_UUID); BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID); bluetoothGatt.readCharacteristic(characteristic); bluetoothGatt.writeCharacteristic(characteristic); ``` 7. **断开连接**:完成通信后,断开与设备的连接。 ```java bluetoothGatt.disconnect(); bluetoothGatt.close(); ``` ### BLE开发注意事项 - **广播配置**:在GAP层定义蓝牙设备发出的广播包信息、广播频率、超时时间、发射功率、蓝牙MAC地址、设备SN码等信息。 - **服务配置**:在GATT层配置服务的UUID、不同服务的数据收发profile规范配置[^2]。 - **数据传输**:BLE的数据传输效率较低,每次可以传输的数据量较小,单次最多只能传输20个byte。 ### BLE应用场景 BLE技术因其低功耗特性,广泛应用于智能穿戴设备、智能锁、心率测量仪等领域。这些设备通常需要长时间运行,且对电池寿命有较高要求。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值