Skip to content

Instantly share code, notes, and snippets.

@vince-lynch
Last active March 26, 2020 16:43
Show Gist options
  • Save vince-lynch/ba69701b54cb9ca39ec5ae9c69b9a4d7 to your computer and use it in GitHub Desktop.
Save vince-lynch/ba69701b54cb9ca39ec5ae9c69b9a4d7 to your computer and use it in GitHub Desktop.
var MyBluetoothGatt = null;
var DeviceUUID = {
CONSMART_BLE_NOTIFICATION_SERVICE_WRGB_UUID: "0000ffd5-0000-1000-8000-00805f9b34fb",
CONSMART_BLE_NOTIFICATION_CHARACTERISTICS_WRGB_UUID: "0000ffd9-0000-1000-8000-00805f9b34fb"
}
var sendsrcAES = [-53, 2, 5, 5, 16, 8, 35, 1, 2, 0, 5, 85, 34, 1, 18, 19, 20, -54]; //byteArray - needs definition/conversion?
// Battery Life
const SLIC_BLE_READ_SERVICE_BATTERY_UUID = "0000180f-0000-1000-8000-00805f9b34fb";
const SLIC_BLE_READ_CHARACTERISTICS_BATTERY_UUID = "00002a19-0000-1000-8000-00805f9b34fb";
// Unlock/Lock property
const CONSMART_BLE_NOTIFICATION_SERVICE_WRGB_UUID = "0000ffd5-0000-1000-8000-00805f9b34fb";
const CONSMART_BLE_NOTIFICATION_CHARACTERISTICS_WRGB_UUID = "0000ffd9-0000-1000-8000-00805f9b34fb";
var connect = function(){
var options = {
filters: [
{services: [
SLIC_BLE_READ_SERVICE_BATTERY_UUID,
CONSMART_BLE_NOTIFICATION_SERVICE_WRGB_UUID
]},
{namePrefix: 'LOCK'}
],
optionalServices: [
]
}
navigator.bluetooth.requestDevice(options)
.then(device => {
console.log('device', device);
console.log('Connecting to GATT Server...');
bluetoothGatt = device.gatt;
device.gatt.connect().then(discoverServices);
})
}
var GattServices = [];
var DeviceAddress;
var GattCharacteristics = {};
var getAddress = function(){
DeviceAddress = bluetoothGatt.getDevice().getAddress();
}
var discoverServices = function(server){
console.log('Found server - getting services', server);
server.getPrimaryServices().then(services =>{
services.forEach((service, i)=>{
GattServices.push(service);
})
console.log("Got services", services);
discoverCharacteristics(services);
})
}
var startNotification = function(characteristic){
//BluetoothRemoteGATTCharacteristic.startNotifications()
characteristic.startNotifications().then((notifyMsg) =>{
console.log('notifyMsg', notifyMsg);
//readCharacteristic(characteristic);
})
}
function uintToString(uintArray) {
var encodedString = String.fromCharCode.apply(null, uintArray),
decodedString = decodeURIComponent(escape(atob(encodedString)));
return decodedString;
}
var readCharacteristic = function(characteristic){
return new Promise((resolve, reject)=>{
characteristic.readValue().then(function(value) {
let readResult = value.getUint8(0);
console.log('readCharacteristic', readResult);
resolve(value);
}).catch((err)=>{
console.log("READ OPERATION FAILED", err);
resolve();
})
})
}
var writeCharacteristic = function(characteristic, val = str2ab('123412')){
if(characteristic.properties.write || characteristic.properties.writeWithoutResponse){
console.log('Write Characteristic', characteristic);
characteristic.writeValue(val).then(function(res) {
console.log('tried to write to characteristic', res);
}).catch((err)=>{
console.log('tried to write to charac', err);
})
}
}
function ab2str(buf) {
console.log(String.fromCharCode.apply(null, new Uint8Array(buf)));
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
var discoverCharacteristics = function(services){
services.forEach((service, i)=>{
GattCharacteristics[service.uuid] = [];
service.getCharacteristics().then(characteristics =>{
//console.log(characteristics)
characteristics.forEach((characteristic, ix)=>{
//console.log('found characteristic', service.uuid, characteristic);
if(characteristic.uuid == SLIC_BLE_READ_CHARACTERISTICS_BATTERY_UUID){
console.log("found battery characteristic");
readCharacteristic(characteristic);
}
if(characteristic.uuid == CONSMART_BLE_NOTIFICATION_CHARACTERISTICS_WRGB_UUID){
console.log('Found writeable characteristic for writing OPEN command', characteristic);
setAES(characteristic).then(()=>{
setPWD(characteristic).then(()=>{
writeOpen(characteristic);
})
})
//writeOpen(characteristic);
}
GattCharacteristics[service.uuid].push(characteristic);
//startNotification(characteristic);
//readCharacteristic(characteristic);
//writeCharacteristic(characteristic, str2ab('123412'));
//writeCharacteristic(characteristic, str2ab("OPEN"));
})
})
})
}
var pwdIsTrue = false;
var sendsrcAES = [-53, 2, 5, 5, 16, 8, 35, 1, 2, 0, 5, 85, 34, 1, 18, 19, 20, -54];
var srcAES = [2, 5, 5, 16, 8, 35, 1, 2, 0, 5, 85, 34, 1, 18, 19, 20];
var updataSrc = function() {
var sendAES = new Uint8Array(18);
var srcAES = new Uint8Array(16);
var i = 0;
while (i < this.srcAES.length) {
var nextInt = ((Math.floor(Math.random() * 90) + 1) & 255); //((random.nextInt(90) + 1) & 255);
srcAES[i] = nextInt;
i++;
sendAES[i] = nextInt;
}
return {srcAES, sendAES};
}
/* access modifiers changed from: private */
var setAES = function(characteristic) {
return new Promise((resolve, reject)=>{
try {
var sendAES = updataSrc().sendAES;
characteristic.writeValue(sendAES).then(function(res) {
console.log('tried to write AES to notification characteristic', res);
readCharacteristic(characteristic).then(()=>{
// read again before returning
resolve();
})
}).catch((err)=>{
console.log('tried to write to charac', err);
})
} catch (e) {
console.log('setAES error', e)
}
})
}
var openArr = function(){
var bArr = new Uint8Array(10);
bArr[0] = -2;
bArr[1] = 79;
bArr[2] = 80;
bArr[3] = 69;
bArr[4] = 78;
bArr[5] = 0;
bArr[6] = 0;
bArr[7] = 0;
bArr[8] = -16;
bArr[9] = -3;
return bArr;
}
var closeArr = function(){
var bArr2 = new Uint8Array(10);
var arr = [-2, 67, 76, 79, 83, 69, 0, 0, -16, -3];
for(var i = 0; i < 10; i++){
bArr2[i] = arr[i];
}
return bArr2;
}
var writeCharacteristic = function(characteristic, inputBytes){
return new Promise((resolve, reject)=>{
characteristic.writeValue(inputBytes).then(function(res) {
console.log('tried to write to characteristic', res);
}).catch((err)=>{
console.log('tried to write to charac', err);
})
})
}
var setPWD = function(characteristic, pass = "123412") {
return new Promise((resolve, reject)=>{
if (pass.length != 6) {
console.log('Password is wrong length');
}
var pArr = [41, 0, 0, 0, 0, 0, 0, 40];
var bArr = new Uint8Array(8);
var i = 0;
while (i < pass.length) {
bArr[i] = pArr[i];
var i2 = i + 1;
bArr[i2] = pass.substring(i, i2);
i = i2;
}
writeCharacteristic(characteristic, bArr).then((resolve, reject)=>{
readCharacteristic(characteristic).then(()=>{
// read again before returning
resolve();
})
})
})
}
var writeOpen = function(characteristic){
var bArr = openArr();
var bArr2 = closeArr();
var z2 = true;
if(z2){
bArr[8] = 0;
bArr2[8] = 0;
}
console.log('Called WriteOpen with Characteristic, going to try and writeValue', bArr);
characteristic.writeValue(bArr).then(function(res) {
console.log('tried to write to characteristic', res);
readCharacteristic(characteristic).then(()=>{
// read again before returning
resolve();
})
}).catch((err)=>{
console.log('tried to write to charac', err);
})
}
var onConnectionStateChange = function(){
}
// What does this do??
var getData = function(){
writeCharacteristic(
DeviceUUID.CONSMART_BLE_NOTIFICATION_SERVICE_WRGB_UUID,
DeviceUUID.CONSMART_BLE_NOTIFICATION_CHARACTERISTICS_WRGB_UUID,
[102, -16, 0, 119] //new byte[]{102, -16, 0, 119}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment