BLEデバイスにデータを送信する場合、以下の手順で進めます。具体的には、キャラクタリスティックにデータを書き込むことで、BLEデバイスに情報を送信します。
1. キャラクタリスティックの特定
まず、データを送信するためのキャラクタリスティックを特定します。通常、BLEデバイスのサービスとキャラクタリスティックのUUIDは、デバイスの仕様書やドキュメントに記載されています。
例えば、以下のようなUUIDを持つキャラクタリスティックにデータを送信するとします:
String targetCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
2. キャラクタリスティックへの書き込み
特定したキャラクタリスティックにデータを書き込むには、writeメソッドを使用します。書き込むデータはList<int>形式で指定します。
(1) データの準備
送信するデータをList<int>形式に変換します。例えば、文字列を送信する場合:
List<int> dataToSend = "Hello, BLE!".codeUnits.toList();
(2) キャラクタリスティックへの書き込み
writeメソッドを使用してデータを送信します。
void writeToCharacteristic(BluetoothCharacteristic characteristic, List<int> data) async {
try {
await characteristic.write(data);
print("Data sent successfully: $data");
} catch (e) {
print("Failed to send data: $e");
}
}
(3) 特定のキャラクタリスティックに書き込む
サービスとキャラクタリスティックを探索し、特定のキャラクタリスティックにデータを書き込みます。
void discoverAndWriteData(BluetoothDevice device, String targetCharacteristicUuid, List<int> data) async {
List<BluetoothService> services = await device.discoverServices();
for (BluetoothService service in services) {
for (BluetoothCharacteristic characteristic in service.characteristics) {
if (characteristic.uuid.toString() == targetCharacteristicUuid) {
await writeToCharacteristic(characteristic, data);
break;
}
}
}
}
3. UIからのデータ送信
ユーザーがUIを介してデータを送信できるように、ボタンやテキストフィールドを追加します。
(1) テキストフィールドとボタンの追加
TextEditingController _textController = TextEditingController();
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BLE Client'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: _textController,
decoration: InputDecoration(labelText: "Enter data to send"),
),
ElevatedButton(
onPressed: () {
String data = _textController.text;
List<int> dataToSend = data.codeUnits.toList();
discoverAndWriteData(connectedDevice!, targetCharacteristicUuid, dataToSend);
},
child: Text("Send Data"),
),
],
),
),
);
}
(2) データ送信のトリガー
ユーザーがボタンを押したときに、テキストフィールドの内容をBLEデバイスに送信します。
4. 書き込みモードの指定
writeメソッドには、書き込みモードを指定するオプションがあります。以下の2つのモードがあります:
withoutResponse: デバイスからの応答を待たずにデータを送信します。withResponse: デバイスからの応答を待ってから次の処理を進めます。
デフォルトはwithResponseです。withoutResponseを使用する場合は、以下のように指定します。
await characteristic.write(data, withoutResponse: true);
5. エラーハンドリング
データ送信中にエラーが発生する可能性があるため、適切にエラーハンドリングを行います。
void writeToCharacteristic(BluetoothCharacteristic characteristic, List<int> data) async {
try {
await characteristic.write(data);
print("Data sent successfully: $data");
} catch (e) {
print("Failed to send data: $e");
}
}
6. 送信データのフォーマット
BLEデバイスが期待するデータフォーマットに合わせて、送信データを準備します。例えば、数値を送信する場合:
int number = 42;
List<int> dataToSend = [number];
7. デバイスの切断
データ送信後、必要に応じてデバイスとの接続を切断します。
void disconnectDevice() async {
if (connectedDevice != null) {
await connectedDevice!.disconnect();
setState(() {
connectedDevice = null;
status = "Disconnected";
});
}
}
まとめ
BLEデバイスにデータを送信する手順は以下の通りです:
- 送信先のキャラクタリスティックを特定する。
- 送信データを
List<int>形式に変換する。 writeメソッドを使用してデータを送信する。- UIを介してユーザーがデータを送信できるようにする。
