diff options
| author | Bernhard Guillon <Bernhard.Guillon@begu.org> | 2024-12-29 20:29:28 +0100 |
|---|---|---|
| committer | Bernhard Guillon <Bernhard.Guillon@begu.org> | 2024-12-29 20:30:08 +0100 |
| commit | 45c9ca9eb3c0f858cbb2324ae290d39809c80cce (patch) | |
| tree | e003d8ab7e72cef9695f35df3877733a495305b0 /client/dart | |
| parent | 1762c56956bafd6fc0636516b494dfa935ff8aec (diff) | |
| download | wb3s-ble-nebula-galaxy-45c9ca9eb3c0f858cbb2324ae290d39809c80cce.tar.gz wb3s-ble-nebula-galaxy-45c9ca9eb3c0f858cbb2324ae290d39809c80cce.zip | |
client/dart: add simple cli test tool
Diffstat (limited to 'client/dart')
| -rw-r--r-- | client/dart/bin/space_light_cli.dart | 102 | ||||
| -rw-r--r-- | client/dart/pubspec.lock | 69 | ||||
| -rw-r--r-- | client/dart/pubspec.yaml | 10 |
3 files changed, 181 insertions, 0 deletions
diff --git a/client/dart/bin/space_light_cli.dart b/client/dart/bin/space_light_cli.dart new file mode 100644 index 0000000..83b5999 --- /dev/null +++ b/client/dart/bin/space_light_cli.dart @@ -0,0 +1,102 @@ +/* + * Simple cli BLE test client + * + * Copyright (C) 2024 Bernhard Guillon <Bernhard.Guillon@begu.org> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * SPDX: GPL-2.0-or-later + * https://spdx.org/licenses/GPL-2.0-or-later.html + */ + +import 'package:bluez/bluez.dart'; +import 'dart:io'; + +void main(List<String> arguments) async { + + if (arguments.length < 1) { + print("usage: space_light RGBCW in hex with"); + print("each of RGB 0..FF"); + print("each of CW 0..64"); + print("example: space_light 0xFF,0x10,0xBA,0x10,0x20"); + exit(-1); + } + + List<int> data = []; + for(var c in arguments[0].split(',')) { + data.add(int.parse(c.split('x').last, radix: 16)); + } + + const String address = "8D:FE:5B:68:D6:D9"; + const String service_uuid = "00000000-0000-1000-8000-00805f9b34fb"; + const String characteristic_uuid = "00000001-0000-1001-8001-00805f9b07d0"; + + var client = BlueZClient(); + await client.connect(); + + + if (client.adapters.isEmpty) { + print('No Bluetooth adapters found'); + await client.close(); + exit(-1); + } + var adapter = client.adapters[0]; + + // FIXME: implement pairing without we have to scan to make + // the device known to hci0 ^^ + print('Searching for devices on ${adapter.name}...'); + for (var device in client.devices) { + print(' ${device.address} ${device.name}'); + } + client.deviceAdded + .listen((device) => print(' ${device.address} ${device.name}')); + + await adapter.startDiscovery(); + await Future.delayed(Duration(seconds: 1)); + await adapter.stopDiscovery(); + + // FIXME: use the device from the scan but as we want to get rid of it anyways keep as is + var dev = client.devices.where((device) => device.address == address); + // TODO: find out if there is something like defer or RAII + if (dev.isEmpty) { + print('Device $address not available'); + await client.close(); + exit(-1); + } + try { + await dev.first.connect(); + } + catch(e) { + print("error connecting.."); + } + + var service = dev.first.gattServices.where((service) => service.uuid.toString() == service_uuid); + // TODO: find out if there is something like defer or RAII + if (service.isEmpty) { + print('service $address not available'); + await client.close(); + exit(-1); + } + var characteristic = service.first.characteristics.where((characteristic) => characteristic.uuid.toString() == characteristic_uuid); + // TODO: find out if there is something like defer or RAII + if (characteristic.isEmpty) { + print('characteristic $characteristic_uuid not available'); + await client.close(); + exit(-1); + } + print("Sending data..."); + await characteristic.first.writeValue(data); + exit(0); +} diff --git a/client/dart/pubspec.lock b/client/dart/pubspec.lock new file mode 100644 index 0000000..17cea19 --- /dev/null +++ b/client/dart/pubspec.lock @@ -0,0 +1,69 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + args: + dependency: transitive + description: + name: args + sha256: bf9f5caeea8d8fe6721a9c358dd8a5c1947b27f1cfaa18b39c301273594919e6 + url: "https://pub.dev" + source: hosted + version: "2.6.0" + bluez: + dependency: "direct main" + description: + name: bluez + sha256: "203a1924e818a9dd74af2b2c7a8f375ab8e5edf0e486bba8f90a0d8a17ed9fce" + url: "https://pub.dev" + source: hosted + version: "0.8.2" + collection: + dependency: transitive + description: + name: collection + sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf + url: "https://pub.dev" + source: hosted + version: "1.19.0" + dbus: + dependency: transitive + description: + name: dbus + sha256: "365c771ac3b0e58845f39ec6deebc76e3276aa9922b0cc60840712094d9047ac" + url: "https://pub.dev" + source: hosted + version: "0.7.10" + ffi: + dependency: transitive + description: + name: ffi + sha256: "16ed7b077ef01ad6170a3d0c57caa4a112a38d7a2ed5602e0aca9ca6f3d98da6" + url: "https://pub.dev" + source: hosted + version: "2.1.3" + meta: + dependency: transitive + description: + name: meta + sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 + url: "https://pub.dev" + source: hosted + version: "1.15.0" + petitparser: + dependency: transitive + description: + name: petitparser + sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27 + url: "https://pub.dev" + source: hosted + version: "6.0.2" + xml: + dependency: transitive + description: + name: xml + sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226 + url: "https://pub.dev" + source: hosted + version: "6.5.0" +sdks: + dart: ">=3.6.0 <4.0.0" diff --git a/client/dart/pubspec.yaml b/client/dart/pubspec.yaml new file mode 100644 index 0000000..5a4e7da --- /dev/null +++ b/client/dart/pubspec.yaml @@ -0,0 +1,10 @@ +name: space_light_cli +description: space light cli. +version: 1.0.0 +# repository: https://github.com/my_org/my_repo + +environment: + sdk: ^3.6.0 + +dependencies: + bluez: ^0.8.2 |
