Skip to content

Commit d2c2571

Browse files
author
Jordy van Kuijk
committed
version 1
1 parent 6b47996 commit d2c2571

13 files changed

Lines changed: 314 additions & 904 deletions

Example/RFDuino/Base.lproj/Main.storyboard

Lines changed: 69 additions & 42 deletions
Large diffs are not rendered by default.

Example/RFDuino/DetailsViewController.swift

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,32 @@
88

99
import UIKit
1010
import RFDuino
11+
import CoreBluetooth
1112

1213
class DetailsViewController: UIViewController {
1314

1415
@IBOutlet weak var titleLabel: UILabel!
1516
@IBOutlet weak var statusLabel: UILabel!
1617
@IBOutlet weak var bluetoothLogo: UIImageView!
1718

19+
@IBOutlet weak var connectButton: UIButton!
20+
@IBOutlet weak var disconnectButton: UIButton!
21+
@IBOutlet weak var discoverButton: UIButton!
22+
@IBOutlet weak var sendDataButton: UIButton!
23+
1824
var rfDuino: RFDuino?
25+
var isAnimating = false
1926
let manager = RFDuinoBTManager.sharedInstance
27+
28+
let bluetoothColor = UIColor(red: 0.04, green: 0.24, blue: 0.57, alpha: 1.0)
2029

2130
override func viewDidLoad() {
2231
super.viewDidLoad()
2332
rfDuino?.delegate = self
2433
manager.delegate = self
2534
titleLabel.text = rfDuino?.name
35+
36+
bluetoothLogo.setImageTintColor(UIColor.blackColor())
2637
}
2738
}
2839

@@ -47,10 +58,17 @@ extension DetailsViewController {
4758

4859
@IBAction func discoverServices(sender: AnyObject) {
4960
statusLabel.text = "discovering services".uppercaseString
61+
if rfDuino!.isConnected {
62+
rfDuino!.discoverServices()
63+
}
5064
}
5165

5266
@IBAction func sendData(sender: UIButton) {
5367
statusLabel.text = "sending data".uppercaseString
68+
if rfDuino!.isConnected {
69+
rfDuino!.send(String("lock").dataUsingEncoding(NSASCIIStringEncoding)!)
70+
rfDuino!.send(String("unlock").dataUsingEncoding(NSASCIIStringEncoding)!)
71+
}
5472
}
5573
}
5674

@@ -62,21 +80,58 @@ extension DetailsViewController: RFDuinoBTManagerDelegate {
6280
func rfDuinoManagerDidConnectRFDuino(manager: RFDuinoBTManager, rfDuino: RFDuino) {
6381
statusLabel.text = "idle".uppercaseString
6482
bluetoothLogo.setImageTintColor(UIColor.greenColor())
65-
}
66-
67-
func rfDuinoManagerDidDisconnectRFDuino(manager: RFDuinoBTManager, rfDuino: RFDuino) {
68-
statusLabel.text = "idle".uppercaseString
69-
bluetoothLogo.setImageTintColor(UIColor.blackColor())
83+
connectButton.enabled = false
84+
disconnectButton.enabled = true
7085
}
7186
}
7287

7388
extension DetailsViewController: RFDuinoDelegate {
89+
90+
func rfDuinoDidDiscoverCharacteristics(rfDuino: RFDuino) {
91+
bluetoothLogo.setImageTintColor(bluetoothColor)
92+
statusLabel.text = "idle".uppercaseString
93+
discoverButton.enabled = false
94+
}
95+
96+
func rfDuinoDidDiscoverServices(rfDuino: RFDuino) {
97+
bluetoothLogo.setImageTintColor(UIColor.blueColor())
98+
statusLabel.text = "idle".uppercaseString
99+
}
100+
101+
func rfDuinoDidSendData(rfDuino: RFDuino, forCharacteristic: CBCharacteristic, error: NSError?) {
102+
if isAnimating {
103+
return
104+
}
105+
isAnimating = true
106+
dispatch_async(dispatch_get_main_queue()) { () -> Void in
107+
self.bluetoothLogo.layer.backgroundColor = UIColor.whiteColor().CGColor
108+
self.bluetoothLogo.layer.cornerRadius = self.bluetoothLogo.frame.size.width / 2
109+
UIView.animateWithDuration(0.3, animations: { () -> Void in
110+
self.bluetoothLogo.layer.backgroundColor = self.bluetoothColor.colorWithAlphaComponent(0.2).CGColor
111+
}) { (bool) -> Void in
112+
self.isAnimating = false
113+
self.bluetoothLogo.layer.backgroundColor = UIColor.whiteColor().CGColor
114+
self.statusLabel.text = "idle".uppercaseString
115+
}
116+
}
117+
}
118+
74119
func rfDuinoDidTimeout(rfDuino: RFDuino) {
75-
bluetoothLogo.setImageTintColor(UIColor.orangeColor())
120+
bluetoothLogo.setImageTintColor(UIColor.redColor())
121+
statusLabel.text = "idle".uppercaseString
76122
}
77123

78124
func rfDuinoDidDiscover(rfDuino: RFDuino) {
79125
bluetoothLogo.setImageTintColor(UIColor.blackColor())
126+
statusLabel.text = "idle".uppercaseString
127+
}
128+
129+
func rfDuinoDidDisconnect(rfDuino: RFDuino) {
130+
connectButton.enabled = true
131+
disconnectButton.enabled = false
132+
discoverButton.enabled = true
133+
statusLabel.text = "idle".uppercaseString
134+
bluetoothLogo.setImageTintColor(UIColor.blackColor())
80135
}
81136
}
82137

Example/RFDuino/DiscoveryTableViewController.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import UIKit
1010
import RFDuino
11+
import CoreBluetooth
1112

1213
class DiscoveryTableViewController: UITableViewController {
1314

@@ -47,10 +48,10 @@ class DiscoveryTableViewController: UITableViewController {
4748

4849
if rfDuino.isTimedOut {
4950
cell?.textLabel?.textColor = UIColor.grayColor()
50-
cell?.textLabel?.text = rfDuino.name! + " (timed out)"
51+
cell?.textLabel?.text = rfDuino.name + " (timed out)"
5152
} else {
5253
cell?.textLabel?.textColor = UIColor.blackColor()
53-
cell?.textLabel?.text = rfDuino.name
54+
cell?.textLabel?.text = rfDuino.name + (rfDuino.RSSI != nil ? " \(rfDuino.RSSI!)" : "")
5455
}
5556

5657
return cell!
@@ -94,4 +95,20 @@ extension DiscoveryTableViewController: RFDuinoDelegate {
9495
func rfDuinoDidDiscover(rfDuino: RFDuino) {
9596
tableView.reloadData()
9697
}
98+
99+
func rfDuinoDidSendData(rfDuino: RFDuino, forCharacteristic: CBCharacteristic, error: NSError?) {
100+
101+
}
102+
103+
func rfDuinoDidDiscoverServices(rfDuino: RFDuino) {
104+
105+
}
106+
107+
func rfDuinoDidDiscoverCharacteristics(rfDuino: RFDuino) {
108+
109+
}
110+
111+
func rfDuinoDidDisconnect(rfDuino: RFDuino) {
112+
113+
}
97114
}

Pod/Classes/RFDuino+Utils.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// RFDuino+Extensions.swift
3+
// Pods
4+
//
5+
// Created by Jordy van Kuijk on 28/01/16.
6+
//
7+
//
8+
9+
import Foundation
10+
import CoreBluetooth
11+
12+
extension Array where Element: RFDuino {
13+
mutating func insertIfNotContained(rfDuino: RFDuino) {
14+
if !(self.contains { return $0.peripheral == rfDuino.peripheral }) {
15+
// append the newly discovered rfDuino
16+
self.append(rfDuino as! Element)
17+
rfDuino.confirmAndTimeout()
18+
} else {
19+
// find existing rfDuino and notify it of rediscovery
20+
let indexOfExistingRFDuino = self.indexOf { return $0.peripheral == rfDuino.peripheral }
21+
let existingRFDuino = self[indexOfExistingRFDuino!]
22+
existingRFDuino.peripheral.delegate = existingRFDuino
23+
existingRFDuino.confirmAndTimeout()
24+
}
25+
}
26+
27+
func findRFDuino(peripheral: CBPeripheral) -> RFDuino? {
28+
let indexOfRFDuino = self.indexOf { return $0.peripheral == peripheral }
29+
if let index = indexOfRFDuino {
30+
return self[index]
31+
} else {
32+
return nil
33+
}
34+
}
35+
}

Pod/Classes/RFDuino-old.swift

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)