This file contains 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 |
This file contains 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
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?){ | |
for characteristic in service.characteristics! { | |
let thisCharacteristic = characteristic as CBCharacteristic | |
if thisCharacteristic.UUID == txCharacteristic{ | |
self.currentCharacteristic = thisCharacteristic | |
let openValue = "1".dataUsingEncoding(NSUTF8StringEncoding)! | |
self.peripheral.writeValue(openValue, forCharacteristic: self.currentCharacteristic, type: CBCharacteristicWriteType.WithResponse) | |
} | |
} | |
if let error = error { |
This file contains 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
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?){ | |
peripheral.discoverCharacteristics(nil, forService: peripheral.services![0]) | |
} |
This file contains 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 |
This file contains 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! | |
override func viewDidLoad() { | |
super.viewDidLoad() |
This file contains 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
// | |
// ViewController.swift | |
// BLESwift | |
// | |
// Created by Ryan Jones on 6/14/16. | |
// Copyright © 2016 Ryan Jones. All rights reserved. | |
// | |
import CoreBluetooth | |
import UIKit |
This file contains 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
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?){ | |
if self.peripheral != nil { | |
self.peripheral.delegate = nil | |
self.peripheral = nil | |
} | |
print("did disconnect", error) | |
self.startManager() | |
} |
This file contains 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
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { | |
peripheral.delegate = self | |
peripheral.discoverServices(nil) | |
print("connected to \(peripheral)") | |
self.stopScan() | |
} | |
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?){ | |
print("connection failed", error) | |
} |
This file contains 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
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) { | |
if (peripheral.name != nil && peripheral.name! == "Device Name"){ | |
self.peripheral = peripheral | |
self.centralManager.connectPeripheral(self.peripheral, options: [CBConnectPeripheralOptionNotifyOnDisconnectionKey : true]) | |
} | |
} |
This file contains 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
func centralManagerDidUpdateState(central: CBCentralManager) { | |
if (central.state == CBCentralManagerState.PoweredOn) { | |
self.centralManager?.scanForPeripheralsWithServices(nil, options: nil) | |
self.currentState.text = "Scanning" | |
} else { | |
print("BLE not on") | |
} | |
} |
NewerOlder