将iPhone用作iBeacon发射器 [英] Using iPhone as iBeacon Transmitter

查看:281
本文介绍了将iPhone用作iBeacon发射器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的iPhone设置为广播iBeacon信号,以便另一部iPhone接听.本来可以接收信号的iPhone正在运行,但是我无法让其他iPhone广播该信号.

I'm trying to set up my iPhone to broadcast an iBeacon signal for another iPhone to pick up. The iPhone that's supposed to pick up the signal is working, but I can't get the other iPhone to broadcast the signal.

我正在关注本教程:这是我的代码:

import CoreBluetooth
import CoreLocation

class iBeaconTransmission: NSObject, CBPeripheralManagerDelegate {

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

    func initLocalBeacon() {
        if localBeacon != nil {
            stopLocalBeacon()
        }

        let localBeaconUUID = "placeholder"
        let localBeaconMajor: CLBeaconMajorValue = 123
        let localBeaconMinor: CLBeaconMinorValue = 456

        let uuid = NSUUID(UUIDString: localBeaconUUID)!
        localBeacon = CLBeaconRegion(proximityUUID: uuid, major: localBeaconMajor, minor: localBeaconMinor, identifier: "placeholder")

        self.beaconPeripheralData = self.localBeacon.peripheralDataWithMeasuredPower(nil)
        self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
    }

    func stopLocalBeacon() {
        peripheralManager.stopAdvertising()
        peripheralManager = nil
        beaconPeripheralData = nil
        localBeacon = nil
    }

    func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
        print("Peripheral Manager")
        if peripheral.state == .PoweredOn {
            print("Transmitting")
            peripheralManager.startAdvertising(beaconPeripheralData as! [String: AnyObject]!)
        } else if peripheral.state == .PoweredOff {
            peripheralManager.stopAdvertising()
        }
    }
}

另一个说明,位置服务已设置为始终授权.

Another note, location services is already set to always authorized.

外围设备管理器"也永远不会被打印出来,因此由于某种原因没有调用其中的功能.

"Peripheral Manager" is also never being printed, so the function that's in isn't being called for some reason.

谢谢!

推荐答案

已修复.我将代码移到了视图控制器中,并在viewDidLoad函数中调用了initLocalBeacon().

Fixed. I moved the code into a view controller and called initLocalBeacon() in the viewDidLoad function.

这篇关于将iPhone用作iBeacon发射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆