Created
July 26, 2016 14:41
-
-
Save ryanjjones10/3105c3f30e80654d16fd7c9386c560ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import CoreBluetooth | |
| import UIKit | |
| class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { | |
| var centralManager: CBCentralManager! | |
| var peripheral: CBPeripheral! | |
| var currentCharacteristic: CBCharacteristic! = nil | |
| var readRSSITimer: NSTimer! | |
| var RSSIholder: NSNumber = 0 | |
| func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { | |
| peripheral.readRSSI() | |
| self.startReadRSSI() | |
| peripheral.delegate = self | |
| peripheral.discoverServices(nil) | |
| print("connected to \(peripheral)") | |
| self.stopScan() | |
| } | |
| func peripheral(peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: NSError?) { | |
| self.RSSIholder = Int(RSSI) | |
| } | |
| func readRSSI(){ | |
| if (self.peripheral != nil){ | |
| self.peripheral.delegate = self | |
| self.peripheral.readRSSI() | |
| } else { | |
| print("peripheral = nil") | |
| } | |
| if (Int(self.RSSIholder) > -70) { | |
| let openValue = "1".dataUsingEncoding(NSUTF8StringEncoding)! | |
| self.peripheral.writeValue(openValue, forCharacteristic: self.currentCharacteristic, type: CBCharacteristicWriteType.WithResponse) | |
| } | |
| } | |
| func startReadRSSI() { | |
| if self.readRSSITimer == nil { | |
| self.readRSSITimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(self.readRSSI), userInfo: nil, repeats: true) | |
| } | |
| } | |
| func stopReadRSSI() { | |
| if (self.readRSSITimer != nil){ | |
| self.readRSSITimer.invalidate() | |
| self.readRSSITimer = nil | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment