大家在iOS 10.0.2上使用iBeacons的情况怎么样?希望比我好!更新:用于测试的硬件是两台iPhone。 iPhone 5S(A1533)和iPhone 7(A1778)。
以下swift 2.x代码的版本可以在iPhone上用于iOS版本9.x广告简单的iBeacon,但是今天,已经更新为swift 3并在iOS 10.0.2上运行的相同代码似乎根本不会广告。
我将代码减少到一个非常简单的辅助类和ViewController,并认为它应该充分证明问题。请注意,Storyboard只有一个连接到启动/停止iBeacon广告的按钮。
我在安装了iOS 10.0.2的iPhone 5和iPhone 7上运行了该应用程序。为了测试,我创建了一个自定义扫描器(嗅探特定的近距离UUID),当那不起作用时,我尝试了更通用的iBeacon检测应用程序,例如Estimote和LightBlue。
叹气-没有什么能看到iBeacon。
你有什么想法吗?苹果开发者论坛提到了关于10.x iBeacon的问题,但据我所知,没有像这样简单的问题。谢谢...
以下swift 2.x代码的版本可以在iPhone上用于iOS版本9.x广告简单的iBeacon,但是今天,已经更新为swift 3并在iOS 10.0.2上运行的相同代码似乎根本不会广告。
我将代码减少到一个非常简单的辅助类和ViewController,并认为它应该充分证明问题。请注意,Storyboard只有一个连接到启动/停止iBeacon广告的按钮。
我在安装了iOS 10.0.2的iPhone 5和iPhone 7上运行了该应用程序。为了测试,我创建了一个自定义扫描器(嗅探特定的近距离UUID),当那不起作用时,我尝试了更通用的iBeacon检测应用程序,例如Estimote和LightBlue。
叹气-没有什么能看到iBeacon。
你有什么想法吗?苹果开发者论坛提到了关于10.x iBeacon的问题,但据我所知,没有像这样简单的问题。谢谢...
- - - iBeaconConfiguration.swift - - - - - -
import Foundation
class iBeaconConfiguration
{
static let uuid = UUID(uuidString: "F34A1A1F-500F-48FB-AFAA-9584D641D7B1")!
private init() {}
}
- - - ViewController.swift - - - - - -
import UIKit
import CoreLocation
import CoreBluetooth
class ViewController: UIViewController, CBPeripheralManagerDelegate
{
@IBOutlet weak var btnAction: UIButton!
let UUID: UUID = iBeaconConfiguration.uuid
var beaconRegion: CLBeaconRegion!
var dataDictionary = NSDictionary()
var bluetoothPeripheralManager: CBPeripheralManager!
var isBroadcasting = false
override func viewDidLoad() {
super.viewDidLoad()
bluetoothPeripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
}
@IBAction func switchBroadcastingState(_ sender: AnyObject)
{
if !isBroadcasting {
if bluetoothPeripheralManager.state == CBManagerState.poweredOn {
let major: CLBeaconMajorValue = 123
let minor: CLBeaconMinorValue = 456
beaconRegion = CLBeaconRegion(proximityUUID: UUID, major: major, minor: minor, identifier: "com.rdz.bcast")
dataDictionary = beaconRegion.peripheralData(withMeasuredPower: nil)
bluetoothPeripheralManager.startAdvertising(dataDictionary as? [String : Any])
btnAction.setTitle("Stop", for: UIControlState.normal)
isBroadcasting = true
}
}
else {
bluetoothPeripheralManager.stopAdvertising()
btnAction.setTitle("Start", for: UIControlState.normal)
isBroadcasting = false
}
}
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
print ("peripheralManagerDidStartAdvertising()")
if error != nil
{
print(error)
}
}
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
print ("peripheralManager(...didReceiveRead)")
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
var statusMessage = ""
switch peripheral.state {
case CBManagerState.poweredOn:
statusMessage = "Bluetooth Status: Turned On"
case CBManagerState.poweredOff:
if isBroadcasting {
switchBroadcastingState(self)
}
statusMessage = "Bluetooth Status: Turned Off"
case CBManagerState.resetting:
statusMessage = "Bluetooth Status: Resetting"
case CBManagerState.unauthorized:
statusMessage = "Bluetooth Status: Not Authorized"
case CBManagerState.unsupported:
statusMessage = "Bluetooth Status: Not Supported"
default:
statusMessage = "Bluetooth Status: Unknown"
}
print("Bluetooth Status: \(statusMessage)")
}
}
- - - END - - - - -
(对于任何代码格式错误,我深表歉意。)