前言
近年来,随着物联网的普及,蓝牙技术也得到了广泛应用。蓝牙技术中的 BLE(Bluetooth Low Energy)是一种低功耗蓝牙技术,被广泛用于物联网设备之间的通信。BLE 协议中的 UUID 扮演了重要的角色,用于标识服务、特征等信息。而标准的 UUID 是由 16 个字节组成的,在实际应用中容易出错,因此出现了 BLE 标准标识符库(ble-standard-identifiers)。本文将介绍如何使用 npm 包 ble-standard-identifiers。
安装
使用 npm 安装 ble-standard-identifiers:
npm install ble-standard-identifiers
使用
使用 ble-standard-identifiers 可以很方便地获取 BLE 服务 UUID、特征 UUID 等标识符。
导入 ble-standard-identifiers:
const BleStandardIdentifiers = require('ble-standard-identifiers');
获取 BLE 服务 UUID:
const uuid = BleStandardIdentifiers.BluetoothServiceUuid.BatteryService; console.log(uuid); // e7810a71-73ae-499d-8c15-faa9aef0c3f2
获取特征 UUID:
const uuid = BleStandardIdentifiers.BluetoothCharacteristicUuid.BatteryLevel; console.log(uuid); // 2a19
深度解析
使用 ble-standard-identifiers 可以方便地获取 BLE 协议中的标准 UUID,但是这些 UUID 的作用并不完全相同,下面对每个 UUID 进行分析。
BLE 服务 UUID
BLE 服务 UUID 用于标识服务,包括标准服务和自定义服务。标准服务包括:
- GAP (Generic Access Profile)
- GATT (Generic Attribute Profile)
- Battery Service
- Blood Pressure Service
- Current Time Service
- Cycling Power Service
- Cycling Speed and Cadence Service
- Device Information Service
- Health Thermometer Service
- Heart Rate Service
- HID (Human Interface Device)
- Immediate Alert Service
- Link Loss Service
- Location and Navigation Service
- Next DST Change Service
- Phone Alert Status Service
- Reference Time Update Service
- Running Speed and Cadence Service
- Scan Parameters Service
- TX Power Service
使用 ble-standard-identifiers 可以很方便地获取这些标准服务的 UUID,例如获取电量服务的 UUID:
const uuid = BleStandardIdentifiers.BluetoothServiceUuid.BatteryService;
BLE 特征 UUID
BLE 特征 UUID 用于标识服务中的特征,包括标准特征和自定义特征。标准特征包括:
- Device Name
- Appearance
- Peripheral Preferred Connection Parameters
- Service Changed
- Battery Level
- Blood Pressure Feature
- Body Sensor Location
- Boot Keyboard Input Report
- Boot Keyboard Output Report
- Boot Mouse Input Report
- Current Time
- Cycling Power Control Point
- Cycling Power Feature
- Cycling Power Measurement
- Cycling Power Vector
- Date Time
- Day of Week
- DST Offset
- Exact Time 256
- Firmware Revision String
- Glucose Feature
- Glucose Measurement
- Glucose Measurement Context
- Hardware Revision String
- Heart Rate Control Point
- Heart Rate Measurement
- HID Control Point
- HID Information
- IEEE 11073-20601 Regulatory Certification Data List
- Intermediate Cuff Pressure
- Intermediate Temperature
- Local Time Information
- Location and Speed
- Manufacturer Name String
- Measurement Interval
- Model Number String
- Navigation
- New Alert
- Peripheral Preferred Connection Parameters
- Peripheral Privacy
- PnP ID
- Position Quality
- Reconnection Address
- Record Access Control Point
- Reference Time Information
- Report
- Report Map
- Ringer Control Point
- Ringer Setting
- Scan Interval Window
- Scan Refresh
- Sensor Location
- Serial Number String
- Software Revision String
- Supported New Alert Category
- Supported Unread Alert Category
- System ID
- Temperature Measurement
- Temperature Type
- Time Accuracy
- Time Source
- Time Update Control Point
- Time Update State
- Time with DST
- Time Zone
- TX Power Level
- Unread Alert Status
使用 ble-standard-identifiers 可以很方便地获取这些标准特征的 UUID,例如获取电量特征的 UUID:
const uuid = BleStandardIdentifiers.BluetoothCharacteristicUuid.BatteryLevel;
教学意义
BLE 技术已经被广泛应用,掌握 BLE 技术对于未来的职业有着很大的帮助。使用 npm 包 ble-standard-identifiers 可以方便地获取 BLE 协议中标准的 UUID,可以减少发生错误的概率,提高开发效率。本文分析了 BLE 协议中 UUID 的作用,可以帮助开发者更好地理解 BLE 技术。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6005607881e8991b448dea36