/* * Simple cli BLE test client * * Copyright (C) 2024 Bernhard Guillon * * 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 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 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); }