iOS

We hope that you have already gained some solid footing on the Eddystone-URL advertisement format. For iOS, Google has already provided a very simple and clearly defined Beacon Scanner Sample app, which shall work out of the box for our Raspberry Pi Beacon.

To download the code for the iOS Eddystone Beacon Scanner Sample from Google (in Swift), navigate to https://github.com/madhurbhargava/eddystone/tree/master/tools/ios-eddystone-scanner-sample/EddystoneScannerSampleSwift.

If you run the app, you would be able to see the Beacon URL being printed in the logs. We do not intend to reinvent the wheel and would urge you to go ahead and download the code from the link mentioned and figure out what it is doing.

We shall brush over some key points to give you a quick start in the iOS Sample (Swift):

  1. The Swift iOS Beacon Scanner Sample code contains the following two important classes:

    1. BeaconScanner.swift: This handles Beacon scanning and data parsing.
    2. Eddystone.swift: This converts the raw Beacon data to Eddystone Beacon object.
  1. We shall only focus on the BeaconScanner class since the Eddystone class is pretty similar to the one we saw in the preceding Android sample. The ViewController class creates a beaconScanner object and calls the startScanning method on the beaconScanner object as shown next:
override func viewDidLoad() {
super.viewDidLoad()
self.beaconScanner = BeaconScanner()
self.beaconScanner!.delegate = self
self.beaconScanner!.startScanning()
}

You can find the preceding code in the ViewController class included in the Swift sample. You can easily compare this to what we do in the onCreate method of the MainActivity file in the Android Sample since this is very much similar.

  1. Post initialization, the BeaconScanner class initiates the search for Eddystone Beacons once the Central Manager (Bluetooth Adapter) is powered on, which it does via the following piece of code:
private func startScanningSynchronized() {
if self.centralManager.state != .PoweredOn {
NSLog("CentralManager state is %d, cannot start scan", self.centralManager.state.rawValue)
self.shouldBeScanning = true
} else {
NSLog("Starting to scan for Eddystones")
let services = [CBUUID(string: "FEAA")]
let options = [CBCentralManagerScanOptionAllowDuplicatesKey : true]
self.centralManager.scanForPeripheralsWithServices(services, options: options)
}
}

You can find the preceding code in the BeaconScanner class.

  1. Once a Eddystone is found, BeaconScanner tries to parse the data associated with the Beacon and this happens in the method mentioned next:
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)

We are afraid that we will be spoiling all the fun for you if we go any further than this, and would request that you go over the code yourself as an exercise to understand what happens after this.

The iOS code is not that very different from the Android code. It basically does the same thing—it searches for the Beacons by navigating to Eddystone service | Beacon Found | Parses Data | Logs DataSummary.

The preceding exercise would not only give you a solid footing, but will also help to make clear that with Android or iOS, whatever may be the platform, the underlying technology (Bluetooth Low Energy) remains the same. The medium of expression (Java versus Swift) might differ a little bit, but the underlying implementations are inherently similar.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.171.121