iOS 低功耗蓝牙 (BLE) 未发现 TX 特性

标签 ios swift arduino bluetooth-lowenergy adafruit

我正在使用 arduino Feather BLE 板,并尝试创建一个可以通过 BLE 向板发送数据的 iOS 应用程序。我可以连接,但无法访问 txCharacteristic

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    guard let characteristics = service.characteristics else {
        return
    }

    for characteristic in characteristics {
        if characteristic.properties.contains(.write) || characteristic.properties.contains(.writeWithoutResponse) {
            writableCharacteristic = characteristic
        }

        //            if(characteristic.uuid == CBUUID(string: "6e400002-b5a3-f393-e0a9-e50e24dcca9e"))
        //            {
        //                txCharacteristic = characteristic
        //            }

        var txUUID = CBUUID(string: "6e400002-b5a3-f393-e0a9-e50e24dcca9e")
        let temp = characteristic.uuid.uuidString;
        switch characteristic.uuid {
        case txUUID:
            txCharacteristic = characteristic;
            break;
        default:
            break;
        }

        peripheral.setNotifyValue(true, for: characteristic)
    }
}

此代码可以工作,但只能发现以下 UUID:

temp    String  "00001532-1212-EFDE-1523-785FEABCD123"  
temp    String  "00001531-1212-EFDE-1523-785FEABCD123"  
temp    String  "00001534-1212-EFDE-1523-785FEABCD123"  

我发现这些 UUID 是 DFU UUID。我如何才能发现 txCharacteristic?

<小时/>

添加了有关我如何调用 discoveryCharacteristics() 的更多信息:

extension SimpleBluetoothIO: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    guard let services = peripheral.services else {
        return
    }

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

最佳答案

通过阅读此网站上的信息,我能够弄清楚这一点: https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout/adding-app-support

特别是这部分(请注意,UART 是服务,TX 和 RX 是服务的特征):

UART Service UUID: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
TX Characteristic UUID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
RX Characteristic UUID: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E

所以我需要使用 UART 服务 ID 调用 Peripheral.discoverServices()。在下面的示例中 self.serviceUUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    //peripheral.discoverServices(nil)
    //peripheral.discoverServices([CBUUID(string: "6e400001-b5a3-f393-e0a9-e50e24dcca9e")])

    // Disover peripheral with service UUID that was given to us during initialization (self.serviceUUID)
    peripheral.discoverServices([CBUUID(string: self.serviceUUID)])
}

然后在 didDiscoverCharacteristicsFor 服务中我需要查找 TX 特征 ID:

// Did Discover Characteristics
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    guard let characteristics = service.characteristics else {
        return
    }

    for characteristic in characteristics {
        if characteristic.properties.contains(.write) || characteristic.properties.contains(.writeWithoutResponse) {
            writableCharacteristic = characteristic
        }

        // TX Characteristic UUID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
        // https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout/adding-app-support
        let txUUID = CBUUID(string: "6e400002-b5a3-f393-e0a9-e50e24dcca9e")
        switch characteristic.uuid {
        case txUUID:
            txCharacteristic = characteristic;
            break;
        default:
            break;
        }

        peripheral.setNotifyValue(true, for: characteristic)
    }
}

关于iOS 低功耗蓝牙 (BLE) 未发现 TX 特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46576345/

相关文章:

arduino - 如何让 Arduino 正常工作而不是在 vscode 上给出 "cannot open source file "avr/pgmspace.h""?

ios - 适用于 iOS 的 Google Analytics - 使用字典发送自定义事件数据作为 createEventWithCategory 仅允许发送 4 个参数

iOS Catalyst Cocoapod 框架错误 - 签名需要开发团队

javascript - Web 编程,简单但适用于机器人元素

Swift 3 CocoaPod 不通过 lint

objective-c - Swift 类中的 NS_ENUM 作为属性

c - 保证 int32_t 和 float 之间的数据保存?

ios - UITableViewCell 的影子问题

iphone - 从 UIALertView 获取即时值(value)

objective-c - 在右栏按钮项的选择器中传递参数