• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 信标没有发现 iOS

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:57:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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 = [CBCentralManagerScanOptionAllowDuplicatesKey:true]
    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: [String : AnyObject], 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("On.")
        break
    case CBCentralManagerState.PoweredOff:
        print("Off.")
        break
    case CBCentralManagerState.Resetting:
        print("Resetting.")
        break
    case CBCentralManagerState.Unauthorized:
        print("Unauthorized.")
        break
    case CBCentralManagerState.Unknown:
        print("Unknown.")
        break
    case CBCentralManagerState.Unsupported:
        print("Unsupported.")
        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("Notifying...")
    }
}

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
    print(characteristic.value)
}
}



Best Answer-推荐答案


无法使用 CoreBluetooth APIsCBCentralManager 类检测 iBeacons,如代码所示。虽然 iBeacon 是蓝牙 LE 设备,但 Apple has security blocks that prevent iOS devices from reading the raw advertisement data使用 didConnectPeripheral 回调。

如果您想在 iOS 上检测 iBeacons,您必须使用 CoreLocation API。您可以阅读有关如何执行此操作的更多信息 here.

关于ios - 信标没有发现 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451595/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap