Skip to content

Instantly share code, notes, and snippets.

@RouNNdeL
Last active November 2, 2019 16:38
Show Gist options
  • Save RouNNdeL/5197b107577c722708461b4ce4686038 to your computer and use it in GitHub Desktop.
Save RouNNdeL/5197b107577c722708461b4ce4686038 to your computer and use it in GitHub Desktop.
Generate BLE standard compliant org.bluetooth.characteristic.current_time characteristic value
/**
* Generate a BLE compliant org.bluetooth.characteristic.current_time characteristic value
*
* @param {Date} date
* @param {Number} reason
*/
function getBleTime(date, reason = 1) {
let str = "";
str += date.getFullYear().toString(16).padStart(4, "0").match(/[a-fA-F0-9]{2}/g).reverse().join('');
str += (date.getMonth() + 1).toString(16).padStart(2, "0");
str += date.getDate().toString(16).padStart(2, "0");
str += date.getHours().toString(16).padStart(2, "0");
str += date.getMinutes().toString(16).padStart(2, "0");
str += date.getSeconds().toString(16).padStart(2, "0");
str += date.getDay().toString(16).padStart(2, "0");
str += (Math.floor(date.getMilliseconds() * 255 / 1000)).toString(16).padStart(2, "0");
str += (reason).toString(16).padStart(2, "0");
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment