Skip to content

Commit 0008cc9

Browse files
authored
Merge pull request #6 from wolever/patch-1
Add --connect/--disconnect options, fail with exit code if connect/disconnect fails
2 parents 23ea22c + e1ac8f1 commit 0008cc9

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

BluetoothConnector.swift

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,68 @@ import IOBluetooth
33
let hardcodedAddress : String? = nil//"12-34-56-78-90-ab"
44

55
func printHelp() {
6-
print("Usage:\nBluetoothConnector 00-00-00-00-00-00\n\nGet the MAC address from the list below (if your device is missing, pair it with your computer first):");
6+
print("Usage:\nBluetoothConnector [-c|--connect|-d|--disconnect] [00-00-00-00-00-00]\n\nGet the MAC address from the list below (if your device is missing, pair it with your computer first):\n");
77
IOBluetoothDevice.pairedDevices().forEach({(device) in
88
guard let device = device as? IOBluetoothDevice,
99
let addressString = device.addressString,
1010
let deviceName = device.name
1111
else { return }
12-
print("\(addressString) - \(deviceName)")
12+
let connectedString = device.isConnected() ? "connected" : "disconnected"
13+
print("\(addressString) - \(deviceName) (\(connectedString))")
1314
})
1415
}
1516

16-
if (CommandLine.arguments.count == 2 || hardcodedAddress != nil) {
17-
let deviceAddress : String? = hardcodedAddress == nil ? CommandLine.arguments[1] : nil
18-
guard let bluetoothDevice = IOBluetoothDevice(addressString: hardcodedAddress ?? deviceAddress) else {
19-
print("Device not found")
20-
exit(-2)
21-
}
22-
23-
if !bluetoothDevice.isPaired() {
24-
print("Not paired to device")
25-
exit(-4)
26-
}
27-
28-
if bluetoothDevice.isConnected() {
29-
bluetoothDevice.closeConnection()
30-
}
31-
else {
32-
bluetoothDevice.openConnection()
17+
var macAddr : String? = nil
18+
var action : String? = nil
19+
20+
for argument in CommandLine.arguments[1...] {
21+
switch argument {
22+
case "-c", "--connect":
23+
action = "connect"
24+
25+
case "-d", "--disconnect":
26+
action = "disconnect"
27+
28+
default:
29+
if argument.hasPrefix("-") || macAddr != nil {
30+
printHelp()
31+
exit(-3)
32+
}
33+
macAddr = argument
3334
}
3435
}
35-
else {
36+
37+
38+
if (macAddr == nil) {
3639
printHelp()
3740
exit(-3)
3841
}
42+
43+
let deviceAddress = hardcodedAddress ?? macAddr!
44+
guard let bluetoothDevice = IOBluetoothDevice(addressString: deviceAddress) else {
45+
print("Device not found: \(deviceAddress)")
46+
exit(-2)
47+
}
48+
49+
if !bluetoothDevice.isPaired() {
50+
print("Not paired to device")
51+
exit(-4)
52+
}
53+
54+
var error : IOReturn = -1
55+
56+
if bluetoothDevice.isConnected() {
57+
if (action == "disconnect" || action == nil) {
58+
error = bluetoothDevice.closeConnection()
59+
}
60+
}
61+
else {
62+
if (action == "connect" || action == nil) {
63+
error = bluetoothDevice.openConnection()
64+
}
65+
}
66+
67+
if error > 0 {
68+
print("\(action ?? "toggle") failed")
69+
exit(-1)
70+
}

0 commit comments

Comments
 (0)