How to correctly set the date and time in a bluetooth low energy peripheral?

后端 未结 3 2124
[愿得一人]
[愿得一人] 2021-02-11 00:26

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

3条回答
  •  爱一瞬间的悲伤
    2021-02-11 00:54

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

提交回复
热议问题