2016-04-22 13 views
1

iOS 9でBLEを検索して接続するアプリを作成しました。しかし、私はiOS 8でそれを実行すると何も起こりません。それは何も見つかりません。なぜそうなのか?私はfunc centralManagerDidUpdateState(central: CBCentralManager)if (central.state == CBCentralManagerState.PoweredOn)ケースで実行する必要がありますね。しかし、私がそれを実行すると、呼び出されないでその機能。 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

答えて

0

障害は、それが動作ここiOSの9で

centralManager.delegate = self 

だったが、iOSの8にこの

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

ようにすべきであり、これさえFUNCに

func initializer() { 
centralManager.delegate = self 
} 
を削除することができます

と書いてこのように書く

override init() { 
    super.init() 
    centralManager = CBCentralManager(delegate: self, queue: nil) 
} 
関連する問題