简体   繁体   English

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

[英]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. 第二个设备是iPad,它具有由LightBlue.app制造的虚拟外围设备。 Sometimes it's called Blank and sometimes it's called iPad, I don't know why. 有时称为Blank,有时称为iPad,我不知道为什么。

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!)中将外设的委托设置为self,以便在发现服务时回叫-

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!) 然后,您将返回到func connectingPeripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!)

swift 3 迅捷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.")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS-如何在BLE外设上发现未发布的服务 - iOS - How to discover unadvertised services on a BLE peripheral 如何使用标识符连接外围设备/ BLE设备? - How to connect peripheral/ BLE device by using identifier? 如何通过CBCentralManager创建到BLE外设的安全连接? - How can I create a secured connection to BLE peripheral through CBCentralManager? 如果我知道iOS中该BLE设备的MAC地址,如何区分该连接到哪个BLE设备外围设备? - How to differentiate to which BLE device's peripheral should I connect If I know mac address of that BLE device in iOS? iPhone可以成为BLE外围设备吗? - Can an iPhone be a BLE peripheral? 外围BLE设备的唯一标识符 - Unique identifier for peripheral BLE device 将iOS 6设备作为BLE外围设备运行 - Run iOS 6 device as a BLE peripheral 如何以编程方式处理BLE外设从iOS设备中删除配对 - How to Programmatically Handle BLE Peripheral Removing Pairing from iOS Device 如何检测外围设备何时停止广告日期,以便我可以删除该外围设备表单已发现设备列表? - How to detect when a peripheral stops advertisement Date so that i can remove that peripheral form discovered device list? 外围设备服务在所有BLE Scanner iOS App中显示为nil,但在android代码中不显示 - Peripheral device services showing nil in all BLE Scanner iOS App but not in android code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM