Cordova Bluetooth module not working with Monaca
-
I am following the instructions from Monaca docs, with example of Bluetooth serial integration code below. But I keep getting error in console saying: “Uncaught TypeError: Cannot read property “isSupported” of undefined”
<script> var g_socketid = -1; var g_bluetoothPlugin = null; var btDevicesSelect = null; var uuidsSelect = null; document.addEventListener('deviceready', function () { console.log("Cordova is now loaded!"); g_bluetoothPlugin = window.bluetooth; //Check if Bluetooth is supported in the device if (!g_bluetoothPlugin.isSupported()) { console.log("Bluetooth is not supported!"); document.getElementById("status").innerHTML = "Bluetooth is not supported!"; } else { //check bluetooth status g_bluetoothPlugin.isEnabled( function(status){ if (status){ document.getElementById("status").innerHTML = "Enabled"; } else { document.getElementById("status").innerHTML = "Disabled"; } }, function(error){ console.log('Fail to get bluetooth status: ' + error); } ); } }, false); function enableBT() { g_bluetoothPlugin.enable( function() { document.getElementById("status").innerHTML = "Enabled"; }, function(error) { console.log( 'Error enabling bluetooth: ' + error ); } ); } function disableBT() { g_bluetoothPlugin.disable( function() { document.getElementById("status").innerHTML = "Disabled"; }, function(error) { console.log( 'Error disabling bluetooth: ' + error ); } ); } function discoverDevices() { console.log('Start discovering'); document.getElementById("discovery_status").innerHTML = "Scanning... Please wait."; var devices = new Array(); g_bluetoothPlugin.startDiscovery( function(device) { devices.push(device); }, function(){ var list = document.getElementById('bt-devices-select'); list.innerHTML = ""; for( var i = 0; i < devices.length; i++ ){ var element = document.createElement('option'); element.value = devices[i].address; element.innerHTML = devices[i].name + "::" + devices[i].address; list.appendChild(element); } btDevicesSelect = new String(devices[0].address); console.log("Detection finished"); document.getElementById("discovery_status").innerHTML = "Scanning Completed."; }, function(error) { console.log( 'Error: ' + JSON.stringify(error) ); }); } function listUUIDs() { console.log("Selected address: " + btDevicesSelect); document.getElementById("listing_status").innerHTML = "Listing... Please wait."; g_bluetoothPlugin.getUuids( function(uuids) { uuidsAll = uuids.uuids; var list = document.getElementById('bt-device-uuids'); for( var i = 0; i < uuidsAll.length; i++ ) { var element = document.createElement('option'); element.value = uuidsAll[i]; element.innerHTML = uuidsAll[i]; list.appendChild(element);console.log(uuidsAll[i]); } uuidsSelect = uuidsAll[uuidsAll.length-1]; console.log( JSON.stringify(uuids) ); document.getElementById("listing_status").innerHTML = "Listing completed."; }, function(error) { console.log( 'Error: ' + JSON.stringify(error) ); }, btDevicesSelect ); } function openRfcomm() { console.log("Selected address: " + btDevicesSelect + " and uuids: " + uuidsSelect); var opts = {'address': btDevicesSelect, 'uuid': uuidsSelect}; g_bluetoothPlugin.connect( function() { document.getElementById("connect_status").innerHTML = "Connected"; }, function(error) { console.log( 'Error:' + error ); document.getElementById("connect_status").innerHTML = "Fail to Connect."; }, opts ); } function closeRfcomm() { g_bluetoothPlugin.disconnect( function() { document.getElementById("connect_status").innerHTML = "Disconnected"; }, function(error) { console.log( 'Error:' + error ); document.getElementById("connect_status").innerHTML = "Fail to disconnect."; } ); } function changeBtSelect(){ var select = document.getElementById('bt-devices-select'); var options = document.getElementById('bt-devices-select').options; btDevicesSelect = options.item(select.selectedIndex).value; console.log(btDevicesSelect); } function changeUuidSelect(){ var select = document.getElementById('bt-device-uuids'); var options = document.getElementById('bt-device-uuids').options; uuidsSelect = options.item(select.selectedIndex).value; } </script>
-
Just in case, I already replied your support ticket about the same subject. Check it out if you already didn’t :)