Created
January 17, 2015 02:49
-
-
Save christophercotton/0993c9702b416dcee242 to your computer and use it in GitHub Desktop.
Display BLE Advertisement Data and when it changes
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 UIKit | |
import CoreBluetooth | |
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { | |
var centralManager:CBCentralManager! | |
var queue:dispatch_queue_t! | |
var datas = [String : String]() | |
var peripherals = [CBPeripheral]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
queue = dispatch_queue_create("MyBLE", DISPATCH_QUEUE_SERIAL) | |
centralManager = CBCentralManager(delegate: self, queue: queue); | |
} | |
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) { | |
if (!contains(peripherals, peripheral)) { | |
peripherals.append(peripheral) | |
} | |
let key = peripheral.identifier.UUIDString | |
let data = advertisementData.description | |
if let previous = datas[key] { | |
if (previous != data) { | |
println("Different \(peripheral.name): \(data)") | |
} | |
} else { | |
println("\(peripheral.name): \(data)"); | |
datas[key] = data | |
} | |
} | |
func centralManagerDidUpdateState(central: CBCentralManager!) { | |
println("centralManagerDidUpdateState"); | |
if (central.state == .PoweredOn ) { | |
println("Scanning") | |
centralManager.scanForPeripheralsWithServices(nil, options:[CBCentralManagerScanOptionAllowDuplicatesKey:true]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment