-
Notifications
You must be signed in to change notification settings - Fork 118
/
bluetooth.dart
129 lines (109 loc) · 3.75 KB
/
bluetooth.dart
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2020, Dart | Windows. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Shows retrieval of various information from the high-level monitor
// configuration API.
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
String addressAsString(Array<Uint8> bluetoothAddress) {
final bluetoothAddressList = <int>[];
for (var i = 0; i < 6; i++) {
bluetoothAddressList.add(bluetoothAddress[i]);
}
return bluetoothAddressList
.map((byte) => byte.toRadixString(16).toUpperCase().padLeft(2, '0'))
.join(':');
}
List<int> findBluetoothRadios() {
final findRadioParams = calloc<BLUETOOTH_FIND_RADIO_PARAMS>()
..ref.dwSize = sizeOf<BLUETOOTH_FIND_RADIO_PARAMS>();
final radioHandles = <int>[];
final hRadio = calloc<HANDLE>();
try {
final hEnum = BluetoothFindFirstRadio(findRadioParams, hRadio);
if (hEnum == NULL) {
print('No radios found.');
} else {
radioHandles.add(hRadio.value);
print('Found a radio with handle: ${hRadio.value.toHexString(32)}');
while (BluetoothFindNextRadio(hEnum, hRadio) == TRUE) {
radioHandles.add(hRadio.value);
}
BluetoothFindRadioClose(hEnum);
}
return radioHandles;
} finally {
free(findRadioParams);
free(hRadio);
}
}
void findRadioInfo(int hRadio) {
final radioInfo = calloc<BLUETOOTH_RADIO_INFO>()
..ref.dwSize = sizeOf<BLUETOOTH_RADIO_INFO>();
try {
final res = BluetoothGetRadioInfo(hRadio, radioInfo);
if (res == WIN32_ERROR.ERROR_SUCCESS) {
final bluetoothAddress = addressAsString(radioInfo.ref.address.rgBytes);
print('Radio name: ${radioInfo.ref.szName}');
print('Radio address: $bluetoothAddress');
}
} finally {
free(radioInfo);
}
}
String convertBluetoothAddress(BLUETOOTH_ADDRESS address) {
final bytes = address.rgBytes;
final buffer = StringBuffer();
for (var idx = 0; idx < 6; idx++) {
buffer.write(bytes[idx].toRadixString(16).padLeft(2, '0').toUpperCase());
if (idx < 5) buffer.write(':');
}
return buffer.toString();
}
void printBluetoothDeviceInfo(BLUETOOTH_DEVICE_INFO info) {
print('Device address: ${convertBluetoothAddress(info.Address)}');
print(' Name: ${info.szName}');
print(' Authenticated: ${info.fAuthenticated != FALSE ? 'True' : 'False'}');
print(' Connected: ${info.fConnected != FALSE ? 'True' : 'False'}');
print(' Remembered: ${info.fRemembered != FALSE ? 'True' : 'False'}');
}
void findBluetoothDevices() {
final params = calloc<BLUETOOTH_DEVICE_SEARCH_PARAMS>()
..ref.dwSize = sizeOf<BLUETOOTH_DEVICE_SEARCH_PARAMS>()
..ref.fReturnConnected = TRUE
..ref.fReturnAuthenticated = TRUE
..ref.fReturnRemembered = TRUE
..ref.fReturnUnknown = TRUE
..ref.fIssueInquiry = TRUE
..ref.cTimeoutMultiplier = 1;
final info = calloc<BLUETOOTH_DEVICE_INFO>()
..ref.dwSize = sizeOf<BLUETOOTH_DEVICE_INFO>();
try {
final firstDeviceHandle = BluetoothFindFirstDevice(params, info);
if (firstDeviceHandle != NULL) {
printBluetoothDeviceInfo(info.ref);
while (BluetoothFindNextDevice(firstDeviceHandle, info) == TRUE) {
printBluetoothDeviceInfo(info.ref);
}
BluetoothFindDeviceClose(firstDeviceHandle);
} else {
print('No devices found.');
}
} finally {
free(params);
free(info);
}
}
void main() {
final radioHandles = findBluetoothRadios();
for (final hRadio in radioHandles) {
findRadioInfo(hRadio);
if (BluetoothIsDiscoverable(hRadio) == TRUE) {
print('Bluetooth radio is discoverable.');
} else {
print('Bluetooth radio is not discoverable.');
}
}
findBluetoothDevices();
}