D~DIDI~DIDIDI!!!!

0%

蓝牙笔记

  1. 分类

通常由两种形式

  1. 传统蓝牙BR(Basic Rate),如蓝牙耳机和音响等

  2. 低功耗蓝牙(Bluetooth Low Energy),如蓝牙手环等Iot设备

  3. 数据传输模型

L2CAP  Layer  Logical  Layer  Physical  Layer  L2CAP Channels  Logical Links  Logical Transports  Physical Links  Physical Channel

分为三层,数据发送时由高层逐层向下流动,接收时相反

  1. L2CAP

  2. Logical

Logical层主要完成BLE通信过程中的各种逻辑控制,在一次BLE通信过程中,存在master和slave两方,master主动扫描并发起连接,slave则不断广播等待连接,下图为一次通信过程中master和slave的状态,scanner和davertiser分别表示amster和slave的扫描状态和广播状态,建立连接之后,开始调频通信

Stand By  Scanner  Initiator  Master  CAN RS  ONNECr RE  Frequency H  Stand By  ADV_IND  DIRECT IND  Advertiser  ADV SCAN IND  ADV NONCONN IND  Slave

  1. Physical

Physical层定义了3个广播信道和37个数据通信信道,在两个蓝牙设备进行连接的过程中,先随机挑选一个广播信道进行广播,待建立连接之后,会在37个通信信道上进行跳频通信

Android BLE接口

在TCP的socket通信中,服务端程序运行于某一IP地址上,并持续监听某个port,客户端程序选择服务端的IP和port发起socket连接,经过三次TCP握手之后完成socket连接的建立并使用该socket进行通信.

蓝牙也是如此,需要先扫描走位蓝牙设备,并选择某个BLE设备,此时需用到以下API

接口名称 所属类 接口描述
startScan bluetoothLeScanner 开始扫描周围BLE设备
stopScan bluetoothLeScanner 结束扫描

扫描并完成选择后就需要与之通信,会用到以下API

接口名称 所属类 接口描述
writeCharacteristic BluetoothGatt 读写指定Characteristic
readCharacteristic BluetoothGatt 读写指定Characteristic

在每一个BLE设备中都会有一个对应的profile用于对这个设备进行描述,一个peofile中包含多个service,为该设备提供各种服务,在一个service中又会有多个characteristic,设备提供的service就是靠这些characteristic来完成

33A83S IN * 13  umOuMu  冖 」 一 一 ㄇ 凵

9:40  Devices  BONDED  CONNECTED  NOT BONDED  Generic Access  UI-JD:  PRIMARY SERVICE  Device Name  UI-JD: Ox2AOO  4.3K/s.* Z O  ADVERTISER  CLIENT  DISCONNECT  0000  SERVER  Properties:  READ, WRITE  Value:  Appearance  UI-JD: Ox2A01  Properties:  READ  [0] Unknown  Value:  Peripheral Privacy Flag  UI-JD: Ox2A02  Properties:  READ, WRITE  Value:  Privacy is disabled in this device  Peripheral Preferred Connection Parameters  I-JUD: Ox2A04  Properties:  READ  Value: Connection Interval: 125.00ms - 250.00ms,  Slave Latency: 0,  Supervision Timeout Multiplier: 2000  Reconnection Address  UI-JD: Ox2A03  Properties:  READ, WRITE, WRITE NO RESPONSE  Value:  Generic Attribute  UI-JD:  PRIMARY SERVICE  Service Changed  I-JUD: Ox2A05  Properties:  INDICATE, READ  Descriptors:  Client Characteristic Configuration  I-JUD:  Value:  Indications enabled  Device Information  UI-JD: Ox180A  PRIMARY SERVICE  Serial Number String  I-JUD: Ox2A25  Properties:  READ  Value:  Oc8080b345  Software Revision String  UI-JD: Ox2A28  Properties:  READ  Value:  vo.2.2  System ID  UI-JD: Ox2A23  Properties:  READ  (Ox) 88-0F-10-FF-FE-80-B3-45  Value:  PnP ID  UI-JD: Ox2A50  Properties:  READ  Value:  Bluetooth SIG Company: Anhui Huami Information  Technology Co., Ltd. <0x0157>  Product Id: 36897  Product Version: 36  Weight Scale  UI-JD: Ox181D  PRIMARY SERVICE  Current Time  UI-JD: Ox2A2B  Properties:  READ, WRITE  Value: 1 Jan 2226, 00:10:54, Day of week: Unknown  Fractions: 0 / 256 s  Weight Scale Feature  UI-JD: Ox2A9E  Properties:  READ  Weight Measurement  I-JUD: Ox2A9D  Properties:  INDICATE  Value: Weight: 4.300 kg  Time: 1 Jan 2226,  Descriptors:  Client Characteristic Configuration  I-JUD:  Value:  Indications enabled  Unknown Characteristic  UI-JD: 00002a2f-OOOO-3512-2118-0009af100700  Properties:  NOTIFY, WRITE  Descriptors:  Client Characteristic Configuration  UI-JD:  Value:  Notifications enabled  Unknown Service  UI-JD: 00001530-0000-3512-2118-0009af100700  PRIMARY SERVICE  Unknown Characteristic  UI-JD: 00001531-0000-3512-2118-0009af100700  Properties:  NOTIFY, WRITE  Descriptors:  Client Characteristic Configuration  UI-JD:  Value:  Notifications enabled  Unknown Characteristic  UI-JD: 00001532-0000-3512-2118-0009af100700  Properties:  WRITE NO RESPONSE  Peripheral Preferred Connection Par...  UI-JD: Ox2A04  Properties:  NOTIFY, READ, WRITE  Descriptors:  Client Characteristic Configuration  UI-JD:  Value:  Notifications enabled  Unknown Characteristic  I-JUD: 00001542-0000-3512-2118-0009af100700  Properties:  NOTIFY, WRITE  Descriptors:  Client Characteristic Configuration  I-JUD:  Value:  Notifications enabled