1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);
}
|