低功耗蓝牙(BLE)基础知识概述

1、开篇

低功耗蓝牙的概念以及低功耗蓝牙为什么能做到低功耗,蓝牙基础知识概述经典蓝牙和低功耗蓝牙的区别中就可以看出来了,这里就不多说了。本篇文章主要介绍BLE应用层开发中常见的一些概念以及BLE广播包的格式。

2、相关名词解析

BLE应用开发过程中,我们常常会看到以下一些名词,很多人不知道它们是什么,这里说说我自己的理解。如果读者暂时不能理解以下概念,也没关系,可以看看后面的Android BLE开发实战相关的文章。后续的文章如果看懂了,这些不懂也问题不大。反正我的理解也不一定对~

2.1 主机和从机

主机和从机是相对的概念,又可以称为主设备和从设备、外围设备(Peripheral)和中心设备(Central)、中心和外设等等。举个简单的例子,我们在使用手机搜索、连接和控制家里的BLE设备的时候,我们的手机就是主机,设备就是从机。一个主机可以同时连接多个从机,一个从机同一时间只能被一个主机连接。

2.2 GAP

GAP是Generic Access Profile的缩写,中文译为通用访问配置文件,主机和从机就是它定义的。GAP不仅定义了蓝牙通信的角色,还控制着它们的广播和连接。需要注意的是,某些设备可以配置成只发送广播而不支持连接,比如一些Beacon设备。

2.3 GATT

GATT是Generic Attribute Profile的缩写,中文译为通用属性配置。
我们使用BLE的时候,主机和从机建立的连接通常称为GATT连接。GATT也给BLE设备定义了角色,GATT Server和GATT Client。通常情况下,主机就是Client,从机就是Server。这是一个C/S架构,主机向从机请求服务。GATT中还有两个尤其重要的概念,Service和Characteristic,一个Service包含多个Characteristic。BLE主从设备之间通信多数情况下就是对特定Service里的特定的Characteristic进行读写操作。每个Service和Characteristic都有一个唯一的UUID与之对应。在开发BLE应用和智能硬件时,两端的开发者需要协商好对应的UUID。

2.4 ATT

ATT是Attribute Protocol,也就是属性协议。它是GATT的下层协议,它把设备的数据定义为一个个的属性,而Service和Characteristic是对属性的逻辑封装。

3、BLE广播包

介绍蓝牙广播包之前先介绍一种LTV数据格式。LTV是Length-Type-Value的缩写,它表示在一个字节数组或者字节流中,数据按照Length -> Type -> Value的顺序拍布。比如数组元素如下(十六进制):

0201060303F1FE

这里包含两组数据:020106和0303F1FE。02作为开始,代表此项数据有两个字节,01代表此项数据的类型,06是此项数据的值。0303F1FE同理。

与LTV类似的数据格式还有TLV,也就是Type-length-Value。

BLE广播包是一个最大有31个字节的数据包,它按LTV格式可以包含多个数据项。接下来介绍几个非常常用的数据类型:

  1. Flags,广播标记位,Type为0x01,Value为一个字节,每个比特位表示设备的一个特性:

    • bit 0: 是否有限发现模式
    • bit 1: 是否普通发现模式
    • bit 2: 是否支持 BR/EDR
    • bit 3: 是否对 Same Device Capable(Controller) 同时支持 BLE 和 BR/EDR
    • bit 4: 是否对 Same Device Capable(Host) 同时支持 BLE 和 BR/EDR
    • bit 5…7: 预留
  2. Service UUID:Service UUID有好多种,完整和不完整的16位、32位和128位UUID。其中最常用的是完整的16位UUID。UUID本身应该是128位的,但是我们可以约定只有其中16位不同,其他的112位都一样,那么我们就可以使用16位来表示一个UUID了,通过扩展即可得到128位UUID。完整的16位UUID对应的Type是0x03。

  3. 设备名称:Type 0x08和0x09分别是设备简称和全称的Type。

  4. 厂商自定义数据(ManufacturerData):0xFF,值得一提的是厂商自定义数据的Value部分的前两个字节为厂商ID,余下的才是真正的数据。

4、附录:全部广播数据类型

在这里插入图片描述

来自:https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile/

  • 8
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是使用C#实现低功耗蓝牙BLE数据读写功能的示例代码: ```C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.GenericAttributeProfile; using Windows.Devices.Enumeration; namespace BleCommunication { class Program { static void Main(string[] args) { var task = MainAsync(args); task.Wait(); } static async Task MainAsync(string[] args) { // 查询所有蓝牙设备 string selector = BluetoothLEDevice.GetDeviceSelector(); var devices = await DeviceInformation.FindAllAsync(selector); // 过滤出指定名称的设备 var device = devices.FirstOrDefault(d => d.Name == "My BLE Device"); if (device != null) { // 连接设备 var bleDevice = await BluetoothLEDevice.FromIdAsync(device.Id); // 查询服务 var serviceUuid = Guid.Parse("0000fff0-0000-1000-8000-00805f9b34fb"); var serviceResult = await bleDevice.GetGattServicesForUuidAsync(serviceUuid); var service = serviceResult.Services.FirstOrDefault(); if (service != null) { // 查询特征值 var characteristicUuid = Guid.Parse("0000fff1-0000-1000-8000-00805f9b34fb"); var characteristicResult = await service.GetCharacteristicsForUuidAsync(characteristicUuid); var characteristic = characteristicResult.Characteristics.FirstOrDefault(); if (characteristic != null) { // 订阅特征值通知 var status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); if (status == GattCommunicationStatus.Success) { characteristic.ValueChanged += (sender, args) => { // 处理接收到的数据 var data = args.CharacteristicValue.ToArray(); Console.WriteLine("Received data: {0}", BitConverter.ToString(data)); }; // 发送数据 var dataToSend = new byte[] { 0x01, 0x02, 0x03 }; var writeStatus = await characteristic.WriteValueAsync(dataToSend.AsBuffer()); if (writeStatus == GattCommunicationStatus.Success) { Console.WriteLine("Data sent successfully."); } else { Console.WriteLine("Failed to send data."); } } else { Console.WriteLine("Failed to subscribe to notifications."); } } else { Console.WriteLine("Characteristic not found."); } } else { Console.WriteLine("Service not found."); } } else { Console.WriteLine("Device not found."); } Console.ReadLine(); } } } ``` 请注意,此示例仅用于演示BLE通信的基本过程。在实际应用中,您需要根据设备的具体规格和特性来编写更复杂的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值