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

问题描述 投票:3回答:3

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

  • ServiceViewer中为“当前时间服务”,它说可以选择写入“当前时间”特征。这意味着GATT客户端(即智能手机)可以通过写入该特征来简单地设置传感器的时间。
  • 然而,在detailed specifications关于该服务,它表示禁止写入该特征。

与详细规格(2011年)相比,服务查看器中的信息更新近(2014年),因此可以安全地假设详细规格尚未更新?

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

有关最佳进展方式的任何线索是什么?

bluetooth bluetooth-lowenergy
3个回答
2
投票

我已经实现了这一点,它将在我身边工作,

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)
}

1
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.