2016-06-24 4 views
1

Monaca IDE에서 Onsenui를 사용하여 블루투스 플러그인을 구현하려고합니다. 다음과 같은 오류 메시지가 계속 나타납니다. bluetoothSerial not found.블루투스 플러그인이있는 Onsenui

Bluetooth Serial 플러그인이 필요한 서비스를 만들고 싶습니다. 그런 다음 이것을 호출하여 .isenabled() 호출을 수행하십시오. 어떤 도움이라도 좋을 것입니다.

답변

0

좋아, 지연에 사과하지만 나에게 약간의 연구가 필요했다.

ons.ready(function(){ 
     bluetoothSerial.isConnected(
      function() { 
       alert("Bluetooth is connected"); 
      }, 
      function() { 
       alert("Bluetooth is not connected"); 
      } 
     );  
    }); 

난 당신이 필요로하는 경우 가져올 수있는 전체 모나카 IDE 프로젝트를 게시 할 수 있습니다 : 모나카 IDE와 플러그인을 설치 및 사용자 정의 안드로이드 빌드를 수행 한 후, 나는 다음과 같은 코드를 사용하여 작동시킬 수 있었다. 그것은 내가 다른 사람들을 도왔던 다른 쓰레드로부터의 많은 다른 코드를 포함 할 것이다. 주목할 중요한 점은 ons.ready을 확인한 다음 변수에 액세스해야한다는 것입니다.

행운과 희망이 Arduino 프로젝트와 함께 당신을 도울 수 있기를 바랍니다!

+0

감사합니다. 도움이 될 것입니다. – condo1234

+0

위의 예를 Onsenui 코드 (내 답변 아래)로 바꾸는 방법을 알고 싶습니다 ... 모바일 앱과의 블루투스 연결이 꽤 인기가있어 많은 사람들이 유용하다고 생각합니다! – condo1234

+0

우리가 작업하기 원하는 코드로 원래 게시물을 편집해야합니다. 대답이 아닌 답변을 올리는 것은 정보를 검색하는 경우 나타나지 않기 때문에 정보를 공유하는 가장 좋은 방법은 아닙니다. – Munsterlander

0

이 코드를 Onsenui에 사용하는 방법.

var app = { 
     initialize: function() { 
      this.bindEvents(); 
      this.showMainPage(); 
     }, 
     bindEvents: function() { 

      var TOUCH_START = 'touchstart'; 
      if (window.navigator.msPointerEnabled) { // windows phone 
       TOUCH_START = 'MSPointerDown'; 
      } 
      document.addEventListener('deviceready', this.onDeviceReady, false); 
      refreshButton.addEventListener(TOUCH_START, this.refreshDeviceList, false); 
      sendButton.addEventListener(TOUCH_START, this.sendData, false); 
      disconnectButton.addEventListener(TOUCH_START, this.disconnect, false); 
      deviceList.addEventListener('touchstart', this.connect, false); 
     }, 
     onDeviceReady: function() { 
      app.refreshDeviceList(); 
     }, 
     refreshDeviceList: function() { 
      bluetoothSerial.list(app.onDeviceList, app.onError); 
     }, 
     onDeviceList: function(devices) { 
      var option; 

      // remove existing devices 
      deviceList.innerHTML = ""; 
      app.setStatus(""); 

      devices.forEach(function(device) { 

       var listItem = document.createElement('li'), 
        html = '<b>' + device.name + '</b><br/>' + device.id; 

       listItem.innerHTML = html; 

       if (cordova.platformId === 'windowsphone') { 
        // This is a temporary hack until I get the list tap working 
        var button = document.createElement('button'); 
        button.innerHTML = "Connect"; 
        button.addEventListener('click', app.connect, false); 
        button.dataset = {}; 
        button.dataset.deviceId = device.id; 
        listItem.appendChild(button); 
       } else { 
        listItem.dataset.deviceId = device.id; 
       } 
       deviceList.appendChild(listItem); 
      }); 

      if (devices.length === 0) { 

       option = document.createElement('option'); 
       option.innerHTML = "No Bluetooth Devices"; 
       deviceList.appendChild(option); 

       if (cordova.platformId === "ios") { // BLE 
        app.setStatus("No Bluetooth Peripherals Discovered."); 
       } else { // Android or Windows Phone 
        app.setStatus("Please Pair a Bluetooth Device."); 
       } 

      } else { 
       app.setStatus("Found " + devices.length + " device" + (devices.length === 1 ? "." : "s.")); 
      } 

     }, 
     connect: function(e) { 
      var onConnect = function() { 
        // subscribe for incoming data 
        bluetoothSerial.subscribe('\n', app.onData, app.onError); 

        resultDiv.innerHTML = ""; 
        app.setStatus("Connected"); 
        app.showDetailPage(); 
       }; 

      var deviceId = e.target.dataset.deviceId; 
      if (!deviceId) { // try the parent 
       deviceId = e.target.parentNode.dataset.deviceId; 
      } 

      bluetoothSerial.connect(deviceId, onConnect, app.onError); 
     }, 
     onData: function(data) { // data received from Arduino 
      console.log(data); 
      resultDiv.innerHTML = resultDiv.innerHTML + "Received: " + data + "<br/>"; 
      resultDiv.scrollTop = resultDiv.scrollHeight; 
     }, 
     sendData: function(event) { // send data to Arduino 

      var success = function() { 
       console.log("success"); 
       resultDiv.innerHTML = resultDiv.innerHTML + "Sent: " + messageInput.value + "<br/>"; 
       resultDiv.scrollTop = resultDiv.scrollHeight; 
      }; 

      var failure = function() { 
       alert("Failed writing data to Bluetooth peripheral"); 
      }; 

      var data = messageInput.value; 
      bluetoothSerial.write(data, success, failure); 
     }, 
     disconnect: function(event) { 
      bluetoothSerial.disconnect(app.showMainPage, app.onError); 
     }, 
     showMainPage: function() { 
      mainPage.style.display = ""; 
      detailPage.style.display = "none"; 
     }, 
     showDetailPage: function() { 
      mainPage.style.display = "none"; 
      detailPage.style.display = ""; 
     }, 
     setStatus: function(message) { 
      console.log(message); 

      window.clearTimeout(app.statusTimeout); 
      statusDiv.innerHTML = message; 
      statusDiv.className = 'fadein'; 

      // automatically clear the status with a timer 
      app.statusTimeout = setTimeout(function() { 
       statusDiv.className = 'fadeout'; 
      }, 5000); 
     }, 
     onError: function(reason) { 
      alert("ERROR: " + reason); // real apps should use notification.alert 
     } 
    }; 
관련 문제