2014-08-29 42 views
2

我會製作應用程序,它允許我從外圍設備下載一些數據。我可以連接外圍設備,但無法下載此設備支持的服務。我沒有第二個作爲外設的應用程序。第二個設備是一個iPad,它具有在LightBlue.app中製作的虛擬外設。有時它被稱爲空白,有時被稱爲iPad,我不知道爲什麼。如何查看外圍設備的BLE服務?

這是我的代碼:

import UIKit 
import CoreBluetooth 

class ViewController: UIViewController, CBCentralManagerDelegate{ 

@IBOutlet var coreBluetooth: UILabel! 
@IBOutlet var discoveredDevices: UILabel! 
@IBOutlet var foundBLE: UILabel! 
@IBOutlet var connected: UILabel! 

var centralManager:CBCentralManager! 
var blueToothReady = false 
var connectingPeripheral:CBPeripheral! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    startUpCentralManager() 
} 

func startUpCentralManager() { 
    centralManager = CBCentralManager(delegate: self, queue: nil) 
} 
func discoverDevices() { 
    centralManager.scanForPeripheralsWithServices(nil, options: nil) 
} 
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: (NSDictionary), RSSI: NSNumber!) { 
    discoveredDevices.text = "Discovered \(peripheral.name)" 
    println("Discovered: \(peripheral.name)") 
    centralManager.stopScan() 

    if peripheral.name == "iPad" || peripheral.name == "Blank" 
    { 
     println("ok") 
     centralManager.connectPeripheral(peripheral, options: nil) 
     self.connectingPeripheral = peripheral 
    } 
} 
func centralManagerDidUpdateState(central: CBCentralManager!) { //BLE status 
    switch (central.state) { 
    case .PoweredOff: 
     coreBluetooth.text = "CoreBluetooth BLE hardware is powered off" 

    case .PoweredOn: 
     coreBluetooth.text = "CoreBluetooth BLE hardware is powered on and ready" 
     blueToothReady = true; 

    case .Resetting: 
     coreBluetooth.text = "CoreBluetooth BLE hardware is resetting" 

    case .Unauthorized: 
     coreBluetooth.text = "CoreBluetooth BLE state is unauthorized" 

    case .Unknown: 
     coreBluetooth.text = "CoreBluetooth BLE state is unknown" 

    case .Unsupported: 
     coreBluetooth.text = "CoreBluetooth BLE hardware is unsupported on this platform" 

    } 
    if blueToothReady { 
     discoverDevices() 
    } 
} 
func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!) 
{ 
    connectingPeripheral.discoverServices(nil) 
    println("Connected") 
    foundBLE.textColor = UIColor.redColor() 
    foundBLE.text = "Connected to: \(peripheral.name)" 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
@IBAction func scanBLE(sender: UIButton) { 
    centralManager.scanForPeripheralsWithServices(nil, options: nil) 
} 

func connectingPeripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) 
{ 
    println("Services \(connectingPeripheral.services)") 
} 

} 
+0

@JasonMArcher。 [我也是,刪除問題](http://data.stackexchange.com/stackoverflow/query/151579/unnecessary-question-labelling),但嘗試修復其他事情時,你談論它。 – TRiG 2014-09-13 22:36:17

+0

@TRiG我會盡力改善這個問題。 – Spamizator 2014-09-14 15:14:09

回答

3

您需要設置外圍的委託,以自己的func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!)爲了得到回調時,服務發現 -

func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!) 
{ 
    connectingPeripheral.delegate=self 
    connectingPeripheral.discoverServices(nil) 
    println("Connected") 
    foundBLE.textColor = UIColor.redColor() 
    foundBLE.text = "Connected to: \(peripheral.name)" 
} 

然後你會撥打電話func connectingPeripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!)

0

swift 3

var peripheralDevice:CBPeripheral! 

    //if connected 
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 

    print("connected") 
    self.peripheralDevice.discoverServices(nil) 
} 

    //if disconnect 
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { 

    print("Disconnect") 
} 

    //fail to connect 
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { 

    print("Fail to connect, Please try again.") 
} 
相關問題