如何查看外围设备的BLE服务?

时间:2014-08-29 13:31:42

标签: ios iphone ipad swift bluetooth-lowenergy

我想制作应用程序,这将允许我从外围设备下载一些数据。我可以连接外围设备,但我无法下载此设备支持的服务。我没有第二个应用程序作为外围设备。第二个设备是iPad,它有一个用LightBlue.app制作的虚拟外围设备。有时它被称为空白,有时它被称为iPad,我不知道为什么。

这是我的代码:

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)")
}

}

2 个答案:

答案 0 :(得分: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!)

答案 1 :(得分:0)

swift 3

<div
      *ngIf="((styleElement.first != 'Visual' && styleElement.first != 'Verbal')
       || (styleElement.first != 'Visual' && styleElement.second != 'Verbal')
       || (styleElement.first != 'Visual' && styleElement.third != 'Verbal'))"
    >