简体   繁体   English

如何在 BLE 上从 IOS 写入当前时间

[英]How to write current time from IOS on BLE

我在任何地方都找不到如何在 BLE 上从 IOS 写入当前时间,如果有任何建议,请回答。

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 = Int8(bitPattern: UInt8(year_mso))
let Year_LSO = Int8(bitPattern: UInt8(year_lso))
let MONTH = Int8(bitPattern: UInt8(comp.month!))
let DAY = Int8(bitPattern: UInt8(comp.day!))
let HOUR = Int8(bitPattern: UInt8(comp.hour!))
let MINUTE = Int8(bitPattern: UInt8(comp.minute!))
let SECOND = Int8(bitPattern: UInt8(comp.second!))
let WEEKDAY = Int8(bitPattern: UInt8(comp.weekday!))
let QUITERVALUE = Int8(bitPattern: UInt8(quiterValue))
let AJRSON = Int8(bitPattern: UInt8(ajjust_reason))

//Current Time Write on tag //当前时间写入标签

let currentTimeArray = [Year_MSO, Year_LSO, MONTH, DAY ,HOUR ,MINUTE ,SECOND_BYTE , WEEKDAY , QUITERVALUE , AJRSON];
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)
    }

swift 3迅捷 3

func setTime(){

let date = Date()
let calendar = Calendar.current
let day = calendar.component(.day, from: date)
let month = calendar.component(.month, from: date)
let year = calendar.component(.year, from: date)
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
self.deviceCurrentTime = "\(month)/\(day)/\(year) \(hour):\(minutes)"

var timeInterval:Int = Int(date.timeIntervalSince1970)

let data = NSData(bytes: &timeInterval, 
          length:MemoryLayout<Int>.size)//df799159

let sendData:Data = data as Data

self.selectPeripheral.writeValue(sendData, for: 
self.writeChar, type: .withResponse)//for set date & time

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM