Reputation: 125
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:
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?
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?
Upvotes: 4
Views: 8460
Reputation: 31
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)
}
Upvotes: 3
Reputation: 2513
The Current Time Service is intended to be a provider of the current time; clients would use it to obtain the time. Hence, having a client write to it does not really make sense. If your device does not have a Real Time Clock then it cannot provide a time service. If your device needs the time and does not have an RTC then your device must become a client to another device which can provide it.
Upvotes: 0
Reputation: 11
Start The time service in iOS Phone. Read the Time from Phone (exposed by service) into sensor. ( for this your iPHone acts as peripheral ) Sensor in this case acts as a Master. Get the data from iPhone and set it into sensor. I hope you can code for Sensor.
Upvotes: 1