read
Bluetooth Low Energy 周辺デバイス上で特性または記述子のデータを読み取る
構文
説明
特性の値の読み取り
は、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 周辺デバイスへの接続を作成します。
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 周辺デバイスへの接続を作成します。
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 周辺デバイスへの接続を作成します。
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
入力引数
Bluetooth Low Energy 周辺デバイスの特性。characteristic
オブジェクトとして指定します。
データを読み取るには、characteristic オブジェクトの Attributes
プロパティに "Read"
、"Notify"
、または "Indicate"
が含まれていなければなりません。
例: data = read(c)
は、characteristic オブジェクト c
の値を読み取ります。
読み取りモード。'latest'
または 'oldest'
として指定します。'latest'
を使用すると、最新のデータを返し、前のデータをフラッシュします。'oldest'
を使用すると、最後の読み取り以降の最も古いデータを返します。
メモ
'oldest'
をコールバック関数 DataAvailableFcn
内で使用すると、前のデータのフラッシュによるエラーが回避されます。
例: data = read(c,'oldest')
は、characteristic オブジェクト c
での最後の読み取り以降の最も古い値を読み取ります。
データ型: char
| string
Bluetooth Low Energy 周辺デバイスの記述子。descriptor
オブジェクトとして指定します。
データを読み取るには、descriptor オブジェクトの Attributes
プロパティに "Read"
が含まれていなければなりません。
例: read(d)
は descriptor オブジェクト d
の値を読み取ります。
出力引数
周辺デバイスからの特性データ。数値または数値の配列として返されます。
データの読み取りは、characteristic オブジェクトの Attributes
プロパティおよび指定されている読み取りmodeに依存します。
c.Attributes | read(c) または read(c,'latest') | read(c,'oldest') |
---|---|---|
| 現在のデータ。 | サポートなし。 |
| 最新の通知または表示のデータ。
| 最後の読み取り以降の最も古い通知または表示。
|
|
|
|
データ型: double
特性または記述子のデータをコンピューターで受信したことを示すタイムスタンプ。datetime
配列として返されます。
データ型: datetime
周辺デバイスからの記述子データ。数値として返されます。
データ型: double
バージョン履歴
R2019b で導入
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)