read
構文
説明
特性の値の読み取り
は、Bluetooth® Low Energy 周辺デバイスから特性の値を読み取ります。データの読み取りは、入力 characteristic オブジェクト characteristicData
= read(c
)c
の Attributes
プロパティに依存します。read
のすべての可能な動作の詳細については、characteristicData
を参照してください。
は、characteristicData
= read(c
,mode
)mode
を読み取りモードとして指定します。
[
は、前述のいずれかの構文でタイムスタンプを読み取ります。characteristicData
,timestamp
] = read(___)
記述子の値の読み取り
は、Bluetooth Low Energy 周辺デバイスから記述子の値を読み取ります。descriptorData
= read(d
)
例
Bluetooth Low Energy 周辺デバイスからの特性データの読み取り
周辺デバイス上の特性にアクセスしてそのデータを読み取ります。
近くにある Bluetooth Low Energy 周辺デバイスへの接続を作成します。
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"
特性を表す characteristic オブジェクトを作成します。
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
データを摂氏単位で解釈します。最初のバイトは温度の整数部を表し、2 番目のバイトは小数部を 0.01 の分解能で表します。
temperature = data(1) + data(2)*0.01
temperature = 23.7300
コールバック関数を使用した Bluetooth Low Energy 周辺デバイスからの特性データの読み取り
周辺デバイス上の特性にアクセスし、そのデータを読み取るコールバック関数を作成します。
近くにある Bluetooth Low Energy 周辺デバイスへの接続を作成します。
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"
特性を表す characteristic オブジェクトを作成します。
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
という名前を付け、以下のように定義します。読み取りモードを 'latest'
ではなく 'oldest'
として指定します。'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)
Bluetooth Low Energy 周辺デバイスからの記述子データの読み取り
周辺デバイス上の記述子にアクセスしてそのデータを読み取ります。
近くにある Bluetooth Low Energy 周辺デバイスへの接続を作成します。
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"
特性を表す characteristic オブジェクトを作成します。
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"
記述子を表す descriptor オブジェクトを作成します。
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 Web サイトの Bluetooth Core Specification にあるこの記述子の仕様を参照して、このデータを解釈します。
通知または表示のステータスが変更されると、この値は変化します。たとえば、subscribe
を使用して通知をサブスクライブします。次に、記述子を再度読み取って、値の変化を確認します。
subscribe(c,'notification');
data = read(d)
data = 1×2
1 0
入力引数
c
— Bluetooth Low Energy 周辺デバイスの特性
characteristic オブジェクト
Bluetooth Low Energy 周辺デバイスの特性。characteristic
オブジェクトとして指定します。
データを読み取るには、characteristic オブジェクトの Attributes
プロパティに "Read"
、"Notify"
、または "Indicate"
が含まれていなければなりません。
例: data = read(c)
は、characteristic オブジェクト c
の値を読み取ります。
mode
— 読み取りモード
'latest'
(既定値) | 'oldest'
読み取りモード。'latest'
または 'oldest'
として指定します。'latest'
を使用すると、最新のデータを返し、前のデータをフラッシュします。'oldest'
を使用すると、最後の読み取り以降の最も古いデータを返します。
メモ
'oldest'
をコールバック関数 DataAvailableFcn
内で使用すると、前のデータのフラッシュによるエラーが回避されます。
例: data = read(c,'oldest')
は、characteristic オブジェクト c
での最後の読み取り以降の最も古い値を読み取ります。
データ型: char
| string
d
— Bluetooth Low Energy 周辺デバイスの記述子
descriptor オブジェクト
Bluetooth Low Energy 周辺デバイスの記述子。descriptor
オブジェクトとして指定します。
データを読み取るには、descriptor オブジェクトの Attributes
プロパティに "Read"
が含まれていなければなりません。
例: read(d)
は descriptor オブジェクト d
の値を読み取ります。
出力引数
characteristicData
— 特性データ
数値
周辺デバイスからの特性データ。数値または数値の配列として返されます。
データの読み取りは、characteristic オブジェクトの Attributes
プロパティおよび指定されている読み取りmodeに依存します。
c.Attributes | read(c) または read(c,'latest') | read(c,'oldest') |
---|---|---|
| 現在のデータ。 | サポートなし。 |
| 最新の通知または表示のデータ。
| 最後の読み取り以降の最も古い通知または表示。
|
|
|
|
データ型: double
timestamp
— タイムスタンプ
datetime
特性または記述子のデータをコンピューターで受信したことを示すタイムスタンプ。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)