1
我在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?) {
}
}
確保您編譯的目標匹配您嘗試運行它的平臺。我有這樣的代碼幾個問題。因此,如果其運行8.x的iPad確保您爲8.x編譯它。 – user3069232