Skip to content

cowbjt/iBeaconBroadcasterDemo

Repository files navigation

TL;DR

An iBeacon Broadcastor

ibeacon senario

Intro

iBeacon

  • A protocol developed by Apple
  • A Bluetooth low energy (BLE) device as Beacon
  • Beacon broadcasts information

iDevices of iBeacon supported

  • iPhone4S or later
  • 3rd generation iPad or later
  • iPad Mini or later
  • 5th generation iPod touch or later

BLE Advertising Packet

Preamble
1Byte
Address
4Bytes
Protocol Data Unit
<=39Bytes
CRC
3Bytes
Header
2Bytes
Payload
0 ~ 37Bytes

Payload

Field Size
UUID 16 bytes
Major 2 bytes
Minor 2 bytes

Example

Store Location San Francisco Paris London
UUID D9B9EC1F-3925-43D0-80A9-1E39D4CEA95C
Major 1 2 3
Minor Clothing 10 10 10
Housewares 20 20 20
Automotive 30 30 30

Programing

Privacy Permission Settings

  • NSBluetoothPeripheralUsageDescription

Check Bluetooth

override func viewDidLoad() {
    super.viewDidLoad()
    peripheralManager = CBPeripheralManager()
    peripheralManager.delegate = self
}

extension BeaconBroadcaster: CBPeripheralManagerDelegate {
    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {

        btEnable = false

        switch peripheral.state {
        case .poweredOff:
            print("poweredOff")
        case .poweredOn:
            print("poweredOn")
            btEnable = true
        case .resetting:
            print("resetting")
        case .unauthorized:
            print("unauthorized")
        case .unsupported:
            print("unsupported")
        case .unknown:
            print("unknown")
        }
    }
}

Start to broadcast

let beaconUuid = UUID(uuidString: defaultUuid)!

let major: CLBeaconMajorValue = 94
let minor: CLBeaconMinorValue = 87
let beaconID = "tw.cowbjt.beacon"

let region = CLBeaconRegion(proximityUUID: beaconUuid, major: major, minor: minor, identifier: beaconID)

let peripheralData = region.peripheralData(withMeasuredPower: nil)

peripheralManager.startAdvertising(((peripheralData as NSDictionary) as! [String : Any]))

Stop broadcasting

peripheralManager.stopAdvertising()

Reference

  1. Turning an iOS Device into an iBeacon

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages