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

Posted

技术标签:

【中文标题】如何查看外围设备的 BLE 服务?【英文标题】:How can I see the BLE services of a peripheral device? 【发布时间】:2014-08-29 13:31:41 【问题描述】:

我想制作一个应用程序,它可以让我从外围设备下载一些数据。我可以连接外围设备,但我无法下载该设备支持的服务。我没有第二个作为外围设备工作的应用程序。第二个设备是 iPad,它有一个用 LightBlue.app 制作的虚拟外围设备。有时叫Blank,有时叫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)")



【问题讨论】:

@JasonMArcher。 I, too, remove Qs,但请务必尝试解决其他问题。 @TRiG 我会努力改进这个问题。 【参考方案1】:

您需要在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)"

然后您将收到回电至func connectingPeripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!)

【讨论】:

【参考方案2】:

斯威夫特 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.")

【讨论】:

以上是关于如何查看外围设备的 BLE 服务?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用标识符连接外围设备/ BLE 设备?

如何创建 Android BLE 外围设备?

XCode:如何通过 UUID 连接到 BLE 外围设备?

低功耗蓝牙Ble的详细使用流程

如何使用标识符连接外围设备/ BLE设备?

如何使用 Core Bluetooth 在 iOS 中设置 BLE 外围设备的名称