菜鸟教程小白 发表于 2022-12-13 11:57:38

ios - 信标没有发现 iOS


                                            <p><p>我正在尝试发现 ibeacon。我在 iPhone 4s 上运行应用程序。设备蓝牙已打开。信标也在其他 iOS 应用程序中发现。
当我在调用委托(delegate)“centralManagerDidUpdateState”之后搜索服务并且状态为“On”之后没有任何反应。预计应该调用“didDiscoverPeripheral”,但没有任何反应。我在这里做错了什么?</p>

<pre><code>Hi,

import UIKit
import FBSDKLoginKit
import SwiftQRCode
import CoreBluetooth

@objc
class DashboardViewController: UIViewController {

var myCentralManager:CBCentralManager!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    myCentralManager = CBCentralManager(delegate: self, queue: nil)
   //      let options =
    myCentralManager.scanForPeripheralsWithServices(nil, options: nil)


}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.hidden = true

}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/
   }


// MARK: CBCentralManagerDelegate
extension DashboardViewController: CBCentralManagerDelegate {

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: , RSSI: NSNumber) {

    peripheral.delegate = self

    //      if peripheral.state ==CBPeripheralState.Disconnected {
    //      
   //         central.connectPeripheral(peripheral, options: nil)
   //      }

    }

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {

    peripheral.delegate = self
    peripheral.discoverServices(nil)

}



func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {

}

func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {

}
}

// MARK: CBCentralManagerDelegate
extension DashboardViewController: CBPeripheralDelegate {

func centralManagerDidUpdateState(central: CBCentralManager) {

    switch central.state{
    case CBCentralManagerState.PoweredOn:
      print(&#34;On.&#34;)
      break
    case CBCentralManagerState.PoweredOff:
      print(&#34;Off.&#34;)
      break
    case CBCentralManagerState.Resetting:
      print(&#34;Resetting.&#34;)
      break
    case CBCentralManagerState.Unauthorized:
      print(&#34;Unauthorized.&#34;)
      break
    case CBCentralManagerState.Unknown:
      print(&#34;Unknown.&#34;)
      break
    case CBCentralManagerState.Unsupported:
      print(&#34;Unsupported.&#34;)
      break
    }
}

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {

    for service in peripheral.services!{
      peripheral.discoverCharacteristics(nil, forService: service)
    }
}

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {

    for charactristics in service.characteristics!{
      peripheral.setNotifyValue(true, forCharacteristic: charactristics)
    }

}

func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?) {

    if characteristic.isNotifying {
      print(&#34;Notifying...&#34;)
    }
}

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
    print(characteristic.value)
}
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>无法使用 <code>CoreBluetooth</code> APIs</strong> 和 <code>CBCentralManager</code> 类检测 iBeacons,如代码所示。虽然 iBeacon 是蓝牙 LE 设备,但 <a href="http://developer.radiusnetworks.com/2013/10/21/corebluetooth-doesnt-let-you-see-ibeacons.html" rel="noreferrer noopener nofollow">Apple has security blocks that prevent iOS devices from reading the raw advertisement data</a>使用 <code>didConnectPeripheral</code> 回调。</p>

<p>如果您想在 iOS 上检测 iBeacons,您必须使用 <code>CoreLocation</code> API。您可以阅读有关如何执行此操作的更多信息 <a href="https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html" rel="noreferrer noopener nofollow">here.</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 信标没有发现 iOS,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33451595/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33451595/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 信标没有发现 iOS