如何在低功耗蓝牙外围设备中正确设置日期和时间



我正在开发一个传感器设备和相应的iOS应用程序,它将使用低功耗蓝牙进行通信。传感器设备需要以实时时钟维护当前日期和时间。现在,如果我想尽可能多地实现蓝牙标准服务,我对在传感器设备中设置时间和日期的正确方法感到困惑,因为官方文档是矛盾的:

    在"当前时间
  • 服务"的服务查看器中,它说可以选择写入"当前时间"特征。这意味着 GATT 客户端(即智能手机)可以通过写入此特性来简单地设置传感器的时间。
  • 但是,在有关该服务的详细规范中,它说禁止写入该特征。

与详细规范 (2011) 相比,服务查看器中的信息要新得多 (2014),因此是否可以安全地假设详细规范尚未更新?

尽管进行了广泛的在线研究,但我找不到有人在BT-LE传感器中设置当前日期和时间的任何示例。

关于最好的方法是什么的任何线索?

我已经实现了这个,它会在我这边工作,

let date = Date()
let calendar = Calendar.current
var comp = calendar.dateComponents([.day, .month, .year, .hour, .minute, .second, .weekday, .nanosecond], from: date)
let milisecond = comp.nanosecond!/1000000
let quiterValue = milisecond/256
let year_mso = comp.year! & 0xFF
let year_lso = (comp.year! >> 8) & 0xFF
let ajjust_reason = 1
let timeZone = calendar.component(.timeZone, from: date)
let DSTZoon = Calendar.current.timeZone.isDaylightSavingTime()
let Year_MSO_Unsinged = Int8(bitPattern: UInt8(year_mso))
let Year_LSO_Unsinged = Int8(bitPattern: UInt8(year_lso))
let MONTH_Unsinged = Int8(bitPattern: UInt8(comp.month!))
let DAY_Unsinged = Int8(bitPattern: UInt8(comp.day!))
let HOUR_Unsinged = Int8(bitPattern: UInt8(comp.hour!))
let MINUTE_Unsinged = Int8(bitPattern: UInt8(comp.minute!))
let SECOND_Unsinged = Int8(bitPattern: UInt8(comp.second!))
let WEEKDAY_Unsinged = Int8(bitPattern: UInt8(comp.weekday!))
let QUITERVALUE_Unsinged = Int8(bitPattern: UInt8(quiterValue))
let AJRSON_Unsinged = Int8(bitPattern: UInt8(ajjust_reason))
//Current Time Write on tag
let currentTimeArray = [Year_MSO_BYTE, Year_LSO_BYTE, MONTH_BYTE, DAY_BYTE ,HOUR_BYTE ,MINUTE_BYTE ,SECOND_BYTE , WEEKDAY_BYTE , QUITERVALUE_BYTE , AJRSON_BYTE];
let currentTimeArray_data = NSData(bytes: currentTimeArray, length: currentTimeArray.length)
if Device.Current_Time != nil {
       deviceValue.peripheral.writeValue(currentTimeArray_data as Data, for:    GrillRightDevice.Current_Time!, type: CBCharacteristicWriteType.withResponse)
}

启动 iOS Phone 中的时间服务。将电话中的时间(由服务公开)读入传感器。(为此,您的iPHone充当外围设备)在这种情况下,传感器充当主设备。从iPhone获取数据并将其设置为传感器。我希望你能为传感器编码。

当前时间服务旨在成为当前时间的提供程序;客户端将使用它来获取时间。因此,让客户端写信给它并没有真正的意义。如果您的设备没有实时时钟,则无法提供时间服务。如果您的设备需要时间并且没有RTC,那么您的设备必须成为可以提供它的另一台设备的客户端。

最新更新