【问题标题】:Core Bluetooth CBAdvertisementDataServiceDataKey could have some custom data?核心蓝牙 CBAdvertisementDataServiceDataKey 可以有一些自定义数据吗?
【发布时间】:2021-04-09 20:46:13
【问题描述】:

我正在尝试使用 BLE 在 android 和 iOS 设备之间发送一些数据,但我需要在 iOS 端向 android 发送一些自定义信息,我已经尝试了 CBCentralManager 提供的几乎所有键,但我尝试使用密钥 CBAdvertisementDataServiceDataKey 发送一些字符串但没有任何反应,有人知道如何在该密钥中发送信息吗?

我会事先尝试在没有连接的情况下获取该信息。

这是我用来尝试制作我之前解释的那部分的函数。

 func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
    
    if peripheral.state == .poweredOn{
        
        let data = CBMutableCharacteristic(type: hardCodeUUID, properties: [.read], value: userName.data(using: .utf8), permissions: [.readable])
     
    
        peripherialManager.startAdvertising([ CBAdvertisementDataLocalNameKey:"Jael2522",CBAdvertisementDataServiceDataKey:userName.data(using: .utf8),CBAdvertisementDataServiceDataKey:[CBUUID(string: "Jose")]])
        
        
        let serialService = CBMutableService(type: hardCodeUUID, primary: true)

        serialService.characteristics = [data]
        peripherialManager.add(serialService)
        
        
    }
    
}

【问题讨论】:

    标签: swift swiftui core-bluetooth


    【解决方案1】:

    CBAdvertisementDataServiceDataKey 在 iOS 中是只读的。您可以直接宣传的唯一内容是本地名称和服务 UUID 列表(两者都不能保证被宣传,但如果它们可以放入数据包中,它们将在您的应用处于前台时进行宣传)。

    为了在设备之间可靠地发送数据,您需要一侧实际连接并读取或写入特征。

    【讨论】:

    • Rob 是不是可以在服务数据中发送一些信息还是不能?
    • 没有。那是 CBAdvertisementDataServiceDataKey,它在 iOS 中是只读的。您可以直接宣传的唯一内容是本地名称和服务 UUID 列表。
    猜你喜欢
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多