2015-11-10 188 views
1

我是新的swift語言。我一直在努力在我的iOS應用程序和條形碼掃描儀之間建立藍牙連接。條碼掃描器啓用了藍牙。我試圖與我的iPhone建立一個全球藍牙連接,它的工作原理。基於互聯網的一些參考文獻,我寫了下面的示例代碼。scanForPeripheralWithServices不返回任何東西

import UIKit 
import CoreBluetooth 

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { 

var manager : CBCentralManager! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    manager = CBCentralManager(delegate : self, queue : nil) 

} 

func centralManagerDidUpdateState(central: CBCentralManager) { 

    var consoleMsg = "hello" 

    switch(central.state) { 

    case .PoweredOff: 
     consoleMsg = "Bluetooth is powered off" 

    case .PoweredOn: 
     consoleMsg = "Bluetooth is powered on" 
     manager.scanForPeripheralsWithServices(nil, options: nil) 

    case .Resetting: 
     consoleMsg = "Bluetooth is Resetting" 

    case .Unauthorized: 
     consoleMsg = "Bluetooth is Unauthorized" 

    case .Unknown: 
     consoleMsg = "Bluetooth is Unknown"    

    case .Unsupported: 
     consoleMsg = "Bluetooth is Unsupported" 

    } 

    print("\(consoleMsg)") 
} 

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) { 

    print("Discovered a peripheral \(peripheral)") 
} 

當我嘗試運行上述代碼時,日誌顯示「藍牙已啓動」,就是這樣。它沒有發現我的條形碼掃描儀。我還確保條形碼掃描儀處於可發現模式。爲什麼我的代碼沒有發現近乎藍牙的條碼掃描器?我在上面的代碼中犯了什麼錯誤?

謝謝

回答

0

您應該首先檢查授權!