在iOS设备上以编程方式测试蓝牙版本

时间:2018-04-07 19:23:57

标签: ios swift bluetooth-lowenergy

有没有办法以编程方式确定某人正在使用的iOS设备(iPhone或iPad)的BLE版本(4.0,4.2,5等)?我需要知道是否应该在应用上启用某些功能,这些功能需要一定的数据传输速率。我正在使用Swift,该应用程序只会与我们硬件中安装的一种BLE模块进行通信。

1 个答案:

答案 0 :(得分:0)

一种选择是保持对各种iPhone型号蓝牙功能的引用,然后通过该型号查找蓝牙版本。

根据Has's answer,您可以像这样获取设备的型号:

func getBluetoothVersion() -> Double{
    let versions: [String: Double] = [
        "iPhone8,1": 4.2,
        "iPhone9,1": 4.2,
        "iPhone10,3": 5.0 //Add as many as you want
    ] 
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let identifier = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8, value != 0 else { return identifier }
        return versions[identifier + String(UnicodeScalar(UInt8(value)))]
    }
}

以上代码已经过测试,但它应该可以正常运行或需要非常小的修复。