Skip to content

Instantly share code, notes, and snippets.

@christophercotton
Created January 17, 2015 02:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophercotton/0993c9702b416dcee242 to your computer and use it in GitHub Desktop.
Save christophercotton/0993c9702b416dcee242 to your computer and use it in GitHub Desktop.
Display BLE Advertisement Data and when it changes
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