Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let totalLength = 2 + 2 + 4 + 4 + 4 + encodedInsert.length
let patch = new ArrayBuffer(totalLength);
let view = new DataView(patch);
view.setUint16(0, totalLength, true);
view.setUint16(2, 2, true);
view.setUint32(4, action.offset, true);
view.setUint32(8, action.oldValue.length, true);
view.setUint32(12, encodedInsert.length, true);
let byteView = new Uint8Array(patch, 16, encodedInsert.length);
byteView.set(encodedInsert);
// React native bridging can't handle Uint8Array so copy into a normal array.
let finalPatch = Array.from(new Uint8Array(patch));
if (state.peripheral_id) {
console.log("writing patch", finalPatch, patch);
BleManager.write(state.peripheral_id, service, contentsCharacteristic, finalPatch).then(() => {
console.log('Wrote patch to device');
});
} else {
console.log("no peripheral", newState.queue);
newState.queue.push(patch);
}
console.log("merging together", action, state.code);
newState.code = state.code.substring(0, action.offset) + action.newValue + state.code.substring(action.offset + action.oldValue.length, state.code.length);
}
console.log("new code state", newState);
return newState;
}
return new Promise( (resolve, reject) =>{
BleManager.write(this.peripheralId, this.writeWithResponseServiceUUID[index], this.writeWithResponseCharacteristicUUID[index], data)
.then(() => {
console.log('Write success: ',data.toString());
resolve();
})
.catch((error) => {
console.log('Write failed: ',data);
reject(error);
});
});
}
BleManager.write(peripheral.id, service, crustCharacteristic, [0]).then(() => {
console.log('Writed NORMAL crust');
BleManager.write(peripheral.id, service, bakeCharacteristic, [1,95]).then(() => {
console.log('Writed 351 temperature, the pizza should be BAKED');
/*
var PizzaBakeResult = {
HALF_BAKED: 0,
BAKED: 1,
CRISPY: 2,
BURNT: 3,
ON_FIRE: 4
};*/
});
});
setTimeout(() => {
BleManager.write(peripheral.id, service, crustCharacteristic, [0]).then(() => {
console.log('Writed NORMAL crust');
BleManager.write(peripheral.id, service, bakeCharacteristic, [1,95]).then(() => {
console.log('Writed 351 temperature, the pizza should be BAKED');
/*
var PizzaBakeResult = {
HALF_BAKED: 0,
BAKED: 1,
CRISPY: 2,
BURNT: 3,
ON_FIRE: 4
};*/
});
});
}, 500);
}).catch((error) => {
setTimeout(() => {
BleManager.write(peripheral.id, service, filenameCharacteristic, stringToBytes("/code.py")).then(() => {
console.log('Wrote filename');
setFileState("nameSet");
});
}, 500);
}).catch((error) => {
setTimeout(() => {
BleManager.write(peripheral.id, service, contentsCharacteristic, command)
.catch((error) => {
console.log('Command error', error);
});
}, 200);
}
attend = () => {
const { user_id, fullname, connected_peripheral } = this.state;
const me = { user_id, fullname };
let str = JSON.stringify(me);
let bytes = bytesCounter.count(str);
let data = stringToBytes(str);
const BASE_UUID = '-5659-402b-aeb3-d2f7dcd1b999';
const PERIPHERAL_ID = '0000';
const PRIMARY_SERVICE_ID = '0100';
let primary_service_uuid = PERIPHERAL_ID + PRIMARY_SERVICE_ID + BASE_UUID;
let ps_characteristic_uuid = PERIPHERAL_ID + '0300' + BASE_UUID;
BleManager.write(connected_peripheral, primary_service_uuid, ps_characteristic_uuid, data, bytes)
.then(() => {
BleManager.disconnect(connected_peripheral)
.then(() => {
Alert.alert('Attended', 'You have successfully attended the event. The app will now close.');
setTimeout(() => {
RNExitApp.exitApp();
}, 3000);
})
.catch((error) => {
Alert.alert('Error disconnecting', "You have successfully attended the event but there's a problem disconnecting to the peripheral, please disable bluetooth to force disconnection.");
});
})
.catch((error) => {
Alert.alert('Error attending', "Something went wrong while trying to attend. Please try again.");