ios - BlueCap Kit BLE 在 iOS 中的实现

标签 ios iphone swift bluetooth-lowenergy

我正在使用 swift 通过 BLE 连接到外围设备并在 iOS 应用程序中收集数据。我使用的包装器最初是由 Troy Stribling 编写的。 https://github.com/troystribling/BlueCap .

我无法采用我的设备特性来匹配他使用的风格。该应用程序工作到它发现设备的程度,但是当我尝试订阅特征以收集数据时它失败了。这是我的 ViewController.swift 代码示例。任何建议将不胜感激:

// discover services and characteristics

    let peripheralDiscoveredFuture = peripheralConnectFuture.flatmap{(peripheral, connectionEvent) -> Future<Peripheral> in
        if peripheral.state == .Connected {
            return peripheral.discoverPeripheralServices([serviceUUID])
    } else {
        let promise = Promise<Peripheral>()
        promise.success(peripheral)
        return promise.future
           }
    } // end of flatmap
    peripheralDiscoveredFuture.onSuccess {peripheral in
        if let service = peripheral.service(serviceUUID) {
            self.HaIdCharacteristic = service.characteristic(hearingAidUUID)
            self.HaBatteryCharacteristic = service.characteristic(batteryUUID)
            self.HaSideCharacteristic = service.characteristic(sideUUID)
            self.SsiNextCharacteristic = service.characteristic(ssiNextUUID)
        }
    }
    // get the characteristic value

    let readEnabledFuture = peripheralDiscoveredFuture.flatmap {__ -> Future<Characteristic> in
        if let HaIdCharacteristic = self.HaIdCharacteristic {
            return HaIdCharacteristic.read(10.0)
        }else {
            let promise = Promise<Characteristic>()
            promise.failure(CentralError.enabledCharacteristicNotFound)
            return promise.future
           }
        }
    readEnabledFuture.onSuccess {characteristic in
        self.updateEnabled(characteristic)
    }

最佳答案

您确定您拥有正确的特征 UUID 吗?您可以使用类似 LightBlue 的应用程序仔细检查。

关于ios - BlueCap Kit BLE 在 iOS 中的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807646/

相关文章:

ios - Xcode 12.4 为模拟器和设备构建,但使用 Pods-MYAPP-frameworks.sh : line 131: ARCHS[@]: unbound variable 归档失败

ios - 是否可以从我的应用程序访问照片编辑扩展程序

iPhone - dequeueReusableCellWithIdentifier 用法

php - iOS Swift 5 - 通过调用 PHP 函数使用服务器上传文件

iOS 10 UNNotificationAction,可以从后台模式切换到前台模式吗?

swift - 这个 View Controller 是否在 "willSet/didSet"对中泄漏?

ios - 在核心数据中保存 sqlite 文件的副本

ios - Swift: 'Protocol'数组和实现类数组之间的转换

iphone - UItabBar 改变 View Controller

c# - 是否可以在 iPhone 和 OS X 上不使用 NIB 文件来创建窗口?