read
语法
说明
读取特征值
从低功耗 Bluetooth® 外围设备读取特征值。读取的数据取决于输入特征对象 characteristicData
= read(c
)c
的 Attributes
属性。有关 read
的所有可能行为的详细信息,请参阅 characteristicData
。
将 characteristicData
= read(c
,mode
)mode
指定为读取模式。
[
读取上述任一语法的时间戳。characteristicData
,timestamp
] = read(___)
读取描述符值
从低功耗 Bluetooth 外围设备读取描述符值。descriptorData
= read(d
)
示例
从低功耗蓝牙外围设备读取特征数据
访问外围设备上的特征并读取其数据。
创建与附近低功耗蓝牙外围设备的连接。
b = ble("Thingy")
b = ble with properties: Name: "Thingy" Address: "F2DF635320F6" Connected: 1 Services: [9×2 table] Characteristics: [38×5 table] Show services and characteristics
创建一个表示 "Temperature"
特征的特征对象。
c = characteristic(b,"Weather Station Service","Temperature")
c = Characteristic with properties: Name: "Temperature" UUID: "EF680201-9B35-4933-9B10-52FFA9740042" Attributes: "Notify" Descriptors: [1x3 table] DataAvailableFcn: [] Show descriptors
由于此特征支持 "Notify"
,您可以使用 read
获取最新数据。
data = read(c)
data = 1×2
23 75
您还可以返回最新数据的时间戳。
[data,timestamp] = read(c)
data = 1×2
23 73
timestamp = datetime
16-May-2019 16:20:00
用摄氏度解释数据。第一个字节表示温度的整数部分,第二个字节表示分辨率为 0.01 的小数部分。
temperature = data(1) + data(2)*0.01
temperature = 23.7300
使用回调函数从低功耗蓝牙外围设备读取特征数据
访问外围设备上的特征,并创建回调函数来读取其数据。
创建与附近低功耗蓝牙外围设备的连接。
b = ble("Thingy")
b = ble with properties: Name: "Thingy" Address: "F2DF635320F6" Connected: 1 Services: [9×2 table] Characteristics: [38×5 table] Show services and characteristics
创建一个表示 "Temperature"
特征的特征对象。
c = characteristic(b,"Weather Station Service","Temperature")
c = Characteristic with properties: Name: "Temperature" UUID: "EF680201-9B35-4933-9B10-52FFA9740042" Attributes: "Notify" Descriptors: [1x3 table] DataAvailableFcn: [] Show descriptors
由于该特征支持 "Notify"
,您可以创建回调函数。将函数命名为 displayCharacteristicData
并定义如下。将读取模式指定为 'oldest'
,而不是 'latest'
。调用 'latest'
数据可能会导致回调函数因刷新以前的数据而出错。
function displayCharacteristicData(src,evt) [data,timestamp] = read(src,'oldest'); disp(data); disp(timestamp); end
使用 @
运算符将函数句柄赋给该特征的 DataAvailableFcn
属性。当有新通知可用时,数据会出现在您的命令行窗口中。
c.DataAvailableFcn = @displayCharacteristicData
c = Characteristic with properties: Name: "Temperature" UUID: "EF680201-9B35-4933-9B10-52FFA9740042" Attributes: "Notify" Descriptors: [1x3 table] DataAvailableFcn: displayCharacteristicData Show descriptors
完成特征处理后,使用 unsubscribe
禁用通知。
unsubscribe(c)
从低功耗蓝牙外围设备读取描述符数据
访问外围设备上的描述符并读取其数据。
创建与附近低功耗蓝牙外围设备的连接。
b = ble("DemoDev")
b = ble with properties: Name: "DemoDev" Address: "FF548EA5658F" Connected: 1 Services: [5×2 table] Characteristics: [10×5 table] Show services and characteristics
创建一个表示 "Heart Rate Measurement"
特征的特征对象。
c = characteristic(b,"Heart Rate","Heart Rate Measurement")
c = Characteristic with properties: Name: "Heart Rate Measurement" UUID: "2A37" Attributes: "Notify" Descriptors: [1x3 table] DataAvailableFcn: [] Show descriptors
创建一个表示 "Client Characteristic Configuration"
描述符的描述符对象。
d = descriptor(c,"Client Characteristic Configuration")
d = Descriptor with properties: Name: "Client Characteristic Configuration" UUID: "2902" Attributes: ["Read" "Write"]
该描述符包含关于通知和指示是启用还是禁用的信息。您可以使用 read
获取当前数据。
data = read(d)
data = 1×2
0 0
请参考 Bluetooth SIG 网站上蓝牙核设定中此描述符的设定来解释这些数据。
当通知或指示状态更改时,此值也会更改。例如,使用 subscribe
订阅通知。然后,通过再次读取描述符来观察值的变化。
subscribe(c,'notification');
data = read(d)
data = 1×2
1 0
输入参数
c
— 低功耗 Bluetooth 外围设备的特征
特征对象
低功耗 Bluetooth 外围设备的特征,指定为 characteristic
对象。
特征对象的 Attributes
属性必须包括 "Read"
、"Notify"
或 "Indicate"
才能读取数据。
示例: data = read(c)
读取特征对象 c
的值。
mode
— 读取模式
'latest'
(默认) | 'oldest'
读取模式,指定为 'latest'
或 'oldest'
。使用 'latest'
返回最新数据并取代以前的数据。使用 'oldest'
返回自上次读取以来最早的数据。
注意
在 DataAvailableFcn
回调函数中使用 'oldest'
以避免因取代先前的数据而导致的错误。
示例: data = read(c,'oldest')
读取自上次读取特征对象 c
以来最早的值。
数据类型: char
| string
d
— 低功耗 Bluetooth 外围设备的描述符
描述符对象
低功耗 Bluetooth 外围设备的描述符,指定为 descriptor
对象。
描述符对象的 Attributes
属性必须包括 "Read"
才能读取数据。
示例: read(d)
读取描述符对象 d
的值。
输出参数
characteristicData
— 特征数据
数值
外围设备的特征数据,以数字或数字数组的形式返回。
读取的数据取决于特征对象的 Attributes
属性和指定的读取mode。
c.Attributes | read(c) 或 read(c,'latest') | read(c,'oldest') |
---|---|---|
| 当前数据。 | 不支持。 |
| 最新通知或指示数据。
| 自上次读取以来最早的通知或指示数据。
|
|
|
|
数据类型: double
timestamp
— Timestamp
日期时间
时间戳,指示在计算机上接收到特征或描述符数据的日期时间,以 datetime
数组形式返回。
数据类型: datetime
descriptorData
— 描述符数据
数值
外围设备的描述符数据,以数字形式返回。
数据类型: double
版本历史记录
在 R2019b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)