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

问题描述:

我在iOS 9上找到并连接到BLE的应用程序。但是当我在iOS 8上运行它时,没有任何反应。它没有找到任何东西。为什么这样?我想它应该在if (central.state == CBCentralManagerState.PoweredOn)的情况下在func centralManagerDidUpdateState(central: CBCentralManager)上运行。但是当我运行它的功能没有被调用。在iOS 9上它可以正常工作,并调用func。iOS 8无法连接到BLE。可以连接到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?) { 

} 
} 
+0

确保您编译的目标匹配您尝试运行它的平台。我有这样的代码几个问题。因此,如果其运行8.x的iPad确保您为8.x编译它。 – user3069232

故障在这里

centralManager.delegate = self 

在iOS系统9它的工作原理,但在iOS的8应该是这样的

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

,甚至可以删除这个FUNC

func initializer() { 
centralManager.delegate = self 
} 

并且像这样写

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