Hi
I’ve made a WebGL app that interact with JavaScript functions of web-page. To begin I made the javescript function is a separate .js file that is called from index.html, now I want to integrate the jave-script functions into the WebGL by moving them to .jspre file in assed folder.
However when I try to build I get compile error for the java-script, which worked perviously when I ran it from index.html and I don’t udnerstand what is wrong with the code.
This is the code:
var bleService = 'cycling_speed_and_cadence'//GATT service requied in sensor
var bleCharacteristic = 'csc_measurement' //GATT characteristic desrited
var bluetoothDeviceDetected //Device object
var gattServer //GATT server object
var gattCharacteristic //GATT characteristic object
//function to check if browser support web-BLT API
function isWebBluetoothEnabled() {
if (!navigator.bluetooth) {
window.alert("Web Bluetooth API is not available in this browser!");
return false;
}
return true;
}
//Function to find and connect to BLE sensor supporting specified GATT service
function getDeviceInfo() {
console.log("Requesting cycling speed and cadence Bluetooth Device...");
return navigator.bluetooth.requestDevice(filters:[{ services: [bleService] }]).then(device => {
bluetoothDeviceDetected = device
console.log("device detected: " + bluetoothDeviceDetected.name)
}).catch(error => {
window.alert("ERROR " + error)
})
}
//Run when pressing connect buttSon
function connect() {
console.log('Web Bluetooth API is available in this browser!')
getDeviceInfo().then(_ => {
return bluetoothDeviceDetected.gatt.connect()
})
.then(server => {
gattServer = server
console.log('Connected to Gatt-server')
})
.then(_ => {
console.log('Getting GATT Service...')
console.log(gattServer)
return gattServer.getPrimaryService(bleService)
})
.then(service => {
console.log('Getting GATT Characteristic...')
return service.getCharacteristic(bleCharacteristic)
})
.then(characteristic => {
gattCharacteristic = characteristic
})
.catch(error => {
console.log('Argh! ' + error)
})
}
//When pressing start, start notification of bike speed measurment
function start() {
gattCharacteristic.startNotifications()
.then(_ => {
gattCharacteristic.addEventListener('characteristicvaluechanged', handleCharacteristicValueChanged)
console.log('Start reading...')
})
.catch (error => {
console.log('Argh! ' + error)
})
}
//When pressing stop, stop notification of bike speed measurment
function stop() {
gattCharacteristic.stopNotifications()
.then(_ => {
console.log('Stop reading...')
})
.catch(error => {
console.log('[ERROR] Stop: ' + error)
})
}
//Called when notification recived from sensor
function handleCharacteristicValueChanged(event) {
const value = event.target.value;
let a = [];
// Convert raw data bytes to hex values just for the sake of showing something.
/*for (let i = 0; i < value.byteLength; i++) {
a.push('0x' + ('00' + value.getUint8(i).toString(16)).slice(-2));
}
console.log('> ' + a.join(' '));
*/
const parsedCscValue = parseCscValue(value)
// console.log(parsedCscValue)
unityInstance.SendMessage('JavascriptHook', 'getSensorReading', JSON.stringify(parsedCscValue));
}
//Parsing function for CSC value
function parseCscValue(value) {
value = value.buffer ? value : new DataView(value); // In Chrome 50+, a DataView is returned instead of an ArrayBuffer.
const flagField = value.getUint8(0)
let result = {}
result.flagField = flagField
let index =1
switch (flagField) {
case 1: //Sensor is Wheel revolution sensor
result.cumulativeWheelRevolutions = value.getUint32(index, /*littleEndian=*/true)
index += 4
result.wheelTimeStamp = value.getUint16(index, /*littleEndian=*/true)
break
case 2: //Sensor is Crank revolution sensor
result.cumulativeCrankRevolutions = value.getUint16(index, /*littleEndian=*/true)
index += 2
result.crankTimeStamp = value.getUint16(index, /*littleEndian=*/true)
break
case 3: //Sensor is Wheel and Crank revolution sensor
result.cumulativeWheelRevolutions = value.getUint32(index, /*littleEndian=*/true)
index += 4
result.wheelTimeStamp = value.getUint16(index, /*littleEndian=*/true)
result.cumulativeCrankRevolutions = value.getUint16(index, /*littleEndian=*/true)
index += 2
result.crankTimeStamp = value.getUint16(index, /*littleEndian=*/true)
break
default: //This should never happen
console.log("error, undefined flagfield value:" + flagField)
}
return result
}
and this is the error I get
Failed running “C:/Program Files/Unity/Hub/Editor/2019.4.18f1/Editor/Data/PlaybackEngines/WebGLSupport\BuildTools\Emscripten_Win\python\2.7.5.3_64bit\python.exe” -E “C:/Program Files/Unity/Hub/Editor/2019.4.18f1/Editor/Data/PlaybackEngines/WebGLSupport\BuildTools\Emscripten\emcc” @“C:\Users\SGAdmin\Documents\GitHub\WebBleSpeedSensorRead\Web-BLE Example Unity App 3\Assets..\Temp\emcc_arguments.resp”
stdout:
stderr:JS optimizer error:Unexpected token punc, expected punc (line: 161, col: 52, pos: 5693)================================}//Function to find and connect to BLE sensor supporting specified GATT servicefunction getDeviceInfo() { console.log(‘Requesting cycling speed and cadence Bluetooth Device…’); return navigator.bluetooth.requestDevice(filters:[{ services: [bleService] }]).then(device => { ^================================C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:282 throw new JS_Parse_Error(message, line, col, pos); ^Error at new JS_Parse_Error (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:260:22) at js_error (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:282:15) at croak (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:752:17) at token_error (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:760:17) at expect_token (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:773:17) at expect (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:776:40) at expr_list (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:1137:56) at subscripts (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:1209:60) at subscripts (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:1201:32) at subscripts (C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:1201:32)ERROR:root:‘C:/Program Files/Unity/Hub/Editor/2019.4.18f1/Editor/Data\Tools\nodejs\node.exe --stack_size=8192 --max-old-space-size=4096 C:\Program Files\Unity\Hub\Editor\2019.4.18f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\js-optimizer.js C:\Users\SGAdmin\AppData\Local\Temp\tmp4dc2_q\build.bc.o.js.pp.js.mem.js noPrintMetadata AJSDCE minifyWhitespace’ failed
UnityEngine.GUIUtility:processEvent(Int32, IntPtr)
Can someone pleas help me understand what’s wrong here.