【问题标题】:Swift 3 Core Bluetooth not discovering servicesSwift 3 Core 蓝牙未发现服务
【发布时间】:2018-03-04 00:34:03
【问题描述】:

我正在尝试从我的 iOS 应用程序连接的 BLE 设备中发现服务,但是当我的 didDiscoverServices 函数被调用时,外围服务数组为空。

应用程序连接到设备,运行 peripheral.discoverServices(nil),然后调用 didDiscoverServices 函数,但没有返回任何服务。

我已经在线阅读了许多与蓝牙相关的答案和其他示例,据我所知,我的代码与应有的代码没有什么不同,只是它不起作用。我觉得我一定是在某个地方错过了什么,但我不确定是什么。

我在下面添加了我的控制台日志,以了解我在运行代码时得到的结果,并在底部添加了蓝牙代码以供参考。

Bluetooth initialised
BLE is powered on
Optional("Nordic_Blinky") found at -72
Scanning stopped
Connect request sent
Connected to <CBPeripheral: 0x1c0301a70, identifier = 0887CF7F-98C8-3FCF-2D10-873FFFFB2B65, name = Nordic_Blinky, state = connected>
Discovering services
Services -- Optional([]) and Error -- nil

我的 BluetoothHandler 类代码如下

class BluetoothHandler : NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {

// MARK: Properties
var manager: CBCentralManager!
var targetPeripheral: CBPeripheral!


// MARK: Shared Instance
static let sharedInstance = BluetoothHandler()
private override init() {
    super.init()
    self.startManager()
    print("Bluetooth initialised")
}

// MARK: Functions

func startManager() {
    manager = CBCentralManager(delegate: self, queue: nil)
}

func centralManagerDidUpdateState(_ central: CBCentralManager) {

    var consoleMessage = ""
    switch (central.state) {

    case.poweredOn:
        consoleMessage = "BLE is powered on"
        manager.scanForPeripherals(withServices: nil, options: nil)

    case.poweredOff:
        consoleMessage = "BLE is powered off"

    case.resetting:
        consoleMessage = "BLE Resetting"

    case.unknown:
        consoleMessage = "BLE state unknown"

    case.unsupported:
        consoleMessage = "Device not supported by BLE"

    case.unauthorized:
        consoleMessage = "BLE not authorised"
    }
    print("\(consoleMessage)")

}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

    if (peripheral.name == "Nordic_Blinky") {

        print("\(String(describing: peripheral.name)) found at \(RSSI)")
        self.stopScan()

        if targetPeripheral != peripheral {

            targetPeripheral = peripheral
            targetPeripheral!.delegate = self

            manager.connect(targetPeripheral, options: nil)
            print("Connect request sent")
        }

    }

    else if (peripheral.name != nil) {
        print("\(String(describing: peripheral.name))")
    }
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    print("Connected to \(peripheral)")
    peripheral.delegate = self
    peripheral.discoverServices(nil)
    print("Discovering services")
}

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Any) {
    print("Connection failed", error)
}

func centralManager(_ central: CBCentralManager, didDisconnect peripheral: CBPeripheral) {

    if self.targetPeripheral != nil {
        self.targetPeripheral!.delegate = nil
        self.targetPeripheral = nil
    }
    print("Connection disconnected")
    self.startManager()
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    print("Services -- \(peripheral.services) and Error -- \(error)")

    if let services = peripheral.services {
        for service in services {
            peripheral.discoverCharacteristics(nil, for: service)
        }
    }
}

【问题讨论】:

  • BlueLight.app 之类的应用是否找到了服务?
  • @Larme 是的,一个 Android 应用确实连接并从设备获取所有服务
  • BlueLight.app 也在 iOS 上?
  • @Larme 它没有显示 iOS 版本的服务,但我不确定它是否缓存了任何东西,所以我无法使用“干净”的 iOS 进行测试设备直到明天或可能星期一
  • 如果 iOS 上的 LightBlue 应用程序未显示任何服务,则问题出在外围设备上。不涉及缓存。

标签: ios swift3 bluetooth core-bluetooth


【解决方案1】:

试试这个:swift 3

如果您的外围设备(BLE 设备)有服务,则在日志中打印。

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {

    if let services = peripheral.services as [CBService]!{

        for service in services{

            peripheral.discoverCharacteristics(nil, for: service)
        }
    }
    print("==>",peripheral.services!)
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

    for newChar: CBCharacteristic in service.characteristics!{

        print(newChar)
    }
}

【讨论】:

  • 我已经在尝试打印要记录的服务,但是它只打印一个空数组,因为没有服务(参见代码和控制台日志)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多