简体   繁体   中英

How can I see the BLE services of a peripheral device?

I would to make app which will allow me to download some data from a peripheral device. I can connect with the peripheral device, but I can't download services which are supported by this device. I don't have second app which works as a peripheral. The second device is an iPad which has a virtual peripheral made in LightBlue.app. Sometimes it's called Blank and sometimes it's called iPad, I don't know why.

This is my code:

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

}

You need to set the peripheral's delegate to self in func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!) in order to get the call back when the services are discovered -

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

You will then get a call back to func connectingPeripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM