Repository navigation
Expand file tree
/
Copy pathBluetoothHelper.swift
More file actions
98 lines (84 loc) · 3.61 KB
/
Copy pathBluetoothHelper.swift
File metadata and controls
98 lines (84 loc) · 3.61 KB
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
//
// BluetoothHelper.swift
//
// Created by Andrew Finke on 8/11/15.
// Copyright (c) 2015 Andrew Finke. All rights reserved.
//
// Used http://pestohacks.blogspot.it/2012/07/make-osx-talks-with-bluetooth-gps.html as a guide
import Cocoa
import IOBluetooth
protocol BluetoothHelperProtocol {
func bluetoothHelperChannelAlmostReady()
func bluetoothHelperFailedToStartChannel()
func bluetoothHelperReceivedString(string: String)
}
class BluetoothHelper: NSObject {
private var address: String!
private var comDevice: IOBluetoothDevice?
private var comChannel: IOBluetoothRFCOMMChannel!
var delegate: BluetoothHelperProtocol?
init(deviceAddress: String) {
super.init()
address = deviceAddress
IOBluetoothDeviceInquiry(delegate: self).start()
}
func deviceInquiryComplete(sender: IOBluetoothDeviceInquiry!, error: IOReturn, aborted: Bool) {
if let comDevice = comDevice {
println("Bluetooth Helper: Found Correct Device (\(comDevice.name))")
if let rfChannelID = findChannelID() {
println("Bluetooth Helper: Found Channel")
var channel: IOBluetoothRFCOMMChannel? = IOBluetoothRFCOMMChannel()
if comDevice.openRFCOMMChannelAsync(&channel, withChannelID: rfChannelID, delegate: self) == kIOReturnSuccess {
println("Bluetooth Helper: Started to Open Channel")
comChannel = channel!
}
else {
println("Bluetooth Helper: Failed To Open Channel")
delegate?.bluetoothHelperFailedToStartChannel()
}
}
else {
println("Bluetooth Helper: Failed To Open Channel")
delegate?.bluetoothHelperFailedToStartChannel()
}
}
else {
println("Bluetooth Helper: Failed To Open Channel")
delegate?.bluetoothHelperFailedToStartChannel()
}
}
func sendMessage(string: NSString) {
println("Bluetooth Helper: Sending Message (\(string))")
let data = string.dataUsingEncoding(NSUTF8StringEncoding)!
comChannel?.writeSync(UnsafeMutablePointer(data.bytes), length: UInt16(data.length))
}
private func findChannelID() -> BluetoothRFCOMMChannelID? {
println("Bluetooth Helper: Finding Channel ID")
let services = comDevice!.services as! [IOBluetoothSDPServiceRecord]
var newChannel = BluetoothRFCOMMChannelID()
for service in services {
if service.getRFCOMMChannelID(&newChannel) == kIOReturnSuccess {
return newChannel
}
}
return nil
}
func deviceInquiryDeviceFound(sender: IOBluetoothDeviceInquiry!, device: IOBluetoothDevice!) {
if device.addressString == address {
comDevice = device
sender.stop()
}
else {
println("Bluetooth Helper: Found Other Device (\(device.name): \(device.addressString))")
}
}
func rfcommChannelOpenComplete(rfcommChannel: IOBluetoothRFCOMMChannel!, status error: IOReturn) {
println("Bluetooth Helper: Channel Almost Ready")
delegate?.bluetoothHelperChannelAlmostReady()
}
func rfcommChannelData(rfcommChannel: IOBluetoothRFCOMMChannel!, data dataPointer: UnsafeMutablePointer<Void>, length dataLength: Int) {
if let dataString = NSString(data: NSData(bytes: dataPointer, length: dataLength), encoding: NSUTF8StringEncoding) as? String {
delegate?.bluetoothHelperReceivedString(dataString)
}
}
}