2

I'm trying to make an iOS app that uses PeripheralManager to broadcast an UUID to a Central.

I think my permissions and Capabilities are in order, but i still cant find my beacon when i search for it with the central.

Is there something i forgot?

import UIKit
import CoreBluetooth
import CoreLocation

class ViewController: UIViewController, CBPeripheralManagerDelegate {

var localBeacon: CLBeaconRegion!
var beaconPeripheralData: NSDictionary!
var peripheralManager: CBPeripheralManager!


override func viewDidLoad() {
    super.viewDidLoad()
    initLocalBeacon()

}

func initLocalBeacon() {

    if localBeacon != nil {
        stopLocalBeacon()
    }

    let localBeaconUUID = "e276c0c0-d740-52e8-b778-9ae6e514269e"
    let localBeaconMajor: CLBeaconMajorValue = 1
    let localBeaconMinor: CLBeaconMinorValue = 1

    let uuid = UUID(uuidString: localBeaconUUID)!
    localBeacon = CLBeaconRegion(proximityUUID: uuid, major: localBeaconMajor, minor: localBeaconMinor, identifier: "Kristers Beacon")

    beaconPeripheralData = localBeacon.peripheralData(withMeasuredPower: -45)

    peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)


}

func stopLocalBeacon() {

    peripheralManager.stopAdvertising()
    peripheralManager = nil
    beaconPeripheralData = nil
    localBeacon = nil
}

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {


    if peripheral.state == .poweredOn {
        peripheralManager.startAdvertising(beaconPeripheralData as! [String: AnyObject]!)

    } else if peripheral.state == .poweredOff {
        stopLocalBeacon()
    }
}


}
Krister Moen
  • 438
  • 2
  • 15
  • 1
    You can't discover an iBeacon using `CBCentralManager`; you need to use CoreLocation and monitor a `CLBeaconRegion` – Paulw11 Apr 03 '18 at 12:14
  • i'm not using CBCentralManager, but HW that runs as central with an C++ bluetooth scanner. – Krister Moen Apr 05 '18 at 14:11
  • Then that is the code you probably need to show. The code you have shown here looks ok. Have you tried a beacon scanner from the App Store or similar on another device to check that it can see your beacon? Have you tried a beacon app from the App Store to confirm that your central can see a beacon from that app? – Paulw11 Apr 05 '18 at 20:50
  • Also, I noticed that you have added background peripheral permission to your app. An iOS app can not advertise a beacon when it is in the background. Are you testing with your app in the foreground? – Paulw11 Apr 05 '18 at 20:53
  • I tested your code and Radius Networks *Locate Beacon* app from the app store could discover it without an issue. I would suggest that you declare `beaconPeripheralData` as `[String:Any]?` and say `beaconPeripheralData = localBeacon.peripheralData(withMeasuredPower: -45) as? [String:Any]` rather than the ugly force downcasts when you start advertising, but the code works both ways. – Paulw11 Apr 05 '18 at 21:07
  • Ok, so you are getting the UUID and the identifier listed in this app? – Krister Moen Apr 09 '18 at 10:16
  • Yes. I cut and pasted your code and then told *Locate Beacon* to look for that uuid. – Paulw11 Apr 09 '18 at 10:19

1 Answers1

0

The problem was the Bluetooth scanner, it did not search for the specific Proximity UUID. The Locate beacon app is great , thanks paul

Krister Moen
  • 438
  • 2
  • 15