Swift: Creating an iBeacon

An iBeacon is a trademark of Apple, which enables a smart phone or other device to perform actions when in close proximity to an iBeacon. The iBeacons are using the new Bluetooth Low Energy (BLE) specification, allowing devices to send permanently packets.

In Swift an iBeacon is created using the following:

let beaconManager: CBPeripheralManager  

This defines the Peripheral Manager.

var data = NSData(bytes: [0xa7, 0xc4, 0xc5, 0xfa, 0xa8, 0xdd, 0x4b, 0xa1, 0xb9, 0xa8, 0xa2, 0x40, 0x58, 0x4f, 0x02, 0xd3, 0x00, 0x04, 0x0f, 0xa0, 0xc5] as Byte[], length: 21)

let dic: NSDictionary = ["kCBAdvDataAppleBeaconKey": data]  
beaconManager.startAdvertising(dic)  

The NSData contains the raw contents for an iBeacon, the iBeacon consists of a uuid, major and minor version number and measured power. The iBeacon advertisement is being started with startAdvertising. After this you'll see that the Mac is advertising the iBeacon.

beaconManager.stopAdvertising()  

The stopAdvertising method will stop advertising the iBeacon.

This code can be used also to mimic other iBeacons, this is not a flaw, but merely by design. More information can be found on the wikipedia page.


References
http://www.blendedcocoa.com/blog/2013/11/02/mavericks-as-an-ibeacon/
http://en.wikipedia.org/wiki/IBeacon
https://developer.apple.com/library/prerelease/ios/documentation/CoreBluetooth/Reference/CBPeripheralManager_Class/index.html