如何在蓝牙低功耗外设中正确设置日期和时间? [英] How to correctly set the date and time in a bluetooth low energy peripheral?

查看:205
本文介绍了如何在蓝牙低功耗外设中正确设置日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a sensor device and a corresponding iOS-app that shall communicate using Bluetooth low energy. The sensor device needs to maintain the current date and time in a real-time clock. Now, I am confused as to what would be the correct way to set the time and date in the sensor device if I want to implement the Bluetooth standard services as much as possible because the official documentation is contradictory:

  • ServiceViewer 对于当前时间服务",它表示可以选择写入当前时间"特征.这意味着GATT客户端(即智能手机)可以通过写入此特性来简单地设置传感器的时间.
  • 关于该服务的详细说明该特性是禁止的.
  • in the ServiceViewer for the "Current Time Service", it says that the "current time" characteristic can optionally be written to. This would mean that the GATT client (i.e., the smartphone) can simply set the sensor's time by writing to this characteristic.
  • in the detailed specifications regarding that service however, it says that writing to that characteristic is forbidden.

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

The information in the Service Viewer is much more recent (2014) compared to the detailed specs (2011), so is it safe to assume that the detailed specs just have not been updated?

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

Despite extensive online research I could not find any example of somebody settings the current date and time in a BT-LE sensor.

关于最佳进行方式的任何线索是什么?

Any clue as to what the best way to proceed would be?

推荐答案

我已经实现了这一点,它将对我有帮助,

I have implement this and it will work my side,

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

这篇关于如何在蓝牙低功耗外设中正确设置日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆