-
Notifications
You must be signed in to change notification settings - Fork 1
/
blueutil.c
73 lines (61 loc) · 1.81 KB
/
blueutil.c
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
/*
* blueutil
* Command-line utility to control Bluetooth.
* Uses private API from IOBluetooth framework (i.e. IOBluetoothPreference*()).
* http://www.frederikseiffert.de/blueutil
*
* This software is public domain. It is provided without any warranty whatsoever,
* and may be modified or used without attribution.
*
* Originally written by Frederik Seiffert <[email protected]>
*
* Convert to C99 and add functions by Brian Reiter <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "bluetooth.h"
#define ON 1
#define OFF 0
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
inline int BTPowerState()
{
return IOBluetoothPreferenceGetControllerPowerState();
}
int BTSetPowerState(int powerState)
{
IOBluetoothPreferenceSetControllerPowerState(powerState);
usleep(2000000); // wait until BT has been set
if (BTPowerState() != powerState) {
printf("Error: unable to turn Bluetooth %s\n", powerState ? "on" : "off");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
void BTStatus()
{
printf("Status: %s\n", BTPowerState() ? "on" : "off");
}
int main(int argc, const char * argv[])
{
int result = EXIT_SUCCESS;
if (!IOBluetoothPreferencesAvailable()) {
printf("Error: Bluetooth not available");
result = EXIT_FAILURE;
} else if (argc == 2 && strcmp(argv[1], "status") == 0) {
BTStatus();
} else if (argc == 2 && strcmp(argv[1], "on") == 0) {
result = BTSetPowerState(ON);
} else if (argc == 2 && strcmp(argv[1], "off") == 0) {
result = BTSetPowerState(OFF);
} else if (argc == 2 && strcmp(argv[1], "restart") == 0) {
IOBluetoothPreferenceSetControllerPowerState(OFF);
usleep(2000000);
result = BTSetPowerState(ON);
} else {
printf("Usage: %s [status|on|off|restart]\n", argv[0]);
result = EXIT_FAILURE;
}
return result;
}