iOS 8 无法连接到 BLE。可以连接到 iOS 9

标签 ios swift core-bluetooth bluetooth-lowenergy

在 iOS 9 上制作了一个应用程序,可以找到并连接到 BLE。但是当我在 iOS 8 上运行它时,什么也没有发生。它没有找到任何东西。我猜它应该在 if (central.state == CBCentralManagerState.PoweredOn) 情况下在 func centralManagerDidUpdateState(central: CBCentralManager) 上运行。但是当我运行它时,那个函数没有被调用。在 iOS 9 上它工作正常并调用该函数。

import CoreBluetooth
import Foundation

class BeaconConnection: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {

private static var connectedBeacon: BeaconConnection?

static func sharedInstance() -> BeaconConnection {
    if connectedBeacon == nil {
        connectedBeacon = BeaconConnection()
    }
    return connectedBeacon!
}

let centralManager = CBCentralManager()
var connectedPeripheral:CBPeripheral?
var peripherals: Array<CBPeripheral> = Array<CBPeripheral>()

func initializer() {
    centralManager.delegate = self
}

func connectToPeripheral(index: Int) {
    print(peripherals[index])
    connectedPeripheral = peripherals[index]
    centralManager.stopScan()
    connectedPeripheral!.delegate = self
    centralManager.connectPeripheral(connectedPeripheral!, options: nil)
}

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
    print(peripheral)
    if !peripherals.contains(peripheral) {
        peripherals.append(peripheral)
    }
}

func centralManagerDidUpdateState(central: CBCentralManager) {
    print("centralManagerDidUpdateState")
    if (central.state == CBCentralManagerState.PoweredOff) {
        print("CoreBluetooth BLE hardware is powered off")
    }
    else if (central.state == CBCentralManagerState.PoweredOn) {
        print("CoreBluetooth BLE hardware is powered on and ready")
        self.centralManager.scanForPeripheralsWithServices(nil, options: nil)
    }
    else if (central.state == CBCentralManagerState.Unauthorized) {
        print("CoreBluetooth BLE state is unauthorized")
    }
    else if (central.state == CBCentralManagerState.Unknown) {
        print("CoreBluetooth BLE state is unknown")
    }
    else if (central.state == CBCentralManagerState.Unsupported) {
        print("CoreBluetooth BLE hardware is unsupported on this platform")
    }
}

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
    print("didConnectPeripheral")
    peripheral.delegate = self
    peripheral.discoverServices(nil)
}

func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
    print("failed to connect")
}

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

func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) {
    print("willRestoreState")
}

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?) {

}
}

最佳答案

错在这里

centralManager.delegate = self

在 iOS 9 中可以,但在 iOS 8 中应该是这样的

centralManager = CBCentralManager(delegate: self, queue: nil)

甚至可以删除这个函数

func initializer() {
centralManager.delegate = self
}

然后这样写

override init() {
    super.init()
    centralManager = CBCentralManager(delegate: self, queue: nil)
}

关于iOS 8 无法连接到 BLE。可以连接到 iOS 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36788638/

相关文章:

ios - Swift - 当 UISearchBar 处于事件状态时转到错误的索引

swift - 按下在函数中以编程方式创建的按钮时执行代码

ios - 我们可以通过蓝牙低功耗在广告数据时发送简单的字符串吗

ios - 如何将 imageData 保存到 NSManagedObject 属性

ios - 容器 View 中的 UICollectionView

ios - 终止app swift 2后发送本地通知

ios - 不删除导航后退按钮标题

ios - 将通过AlamoFire下载的MPEG音频文件转换为mp3文件

ios - 每 10 秒才收到一次来自中央的数据,为什么?

ios - 如何读取特征值?