2016-06-24 10 views
1

Monaca IDEでOnsenuiを使用してBluetoothプラグインを実装しようとしています。私は、エラーメッセージが表示され続ける:bluetoothSerialが見つかりません。Bluetoothプラグイン付きOnsenui

Bluetooth Serialプラグインが必要なサービスを作成したいとします。次に、.isenabled()呼び出しを行うためにこれを呼び出します。どんな助けも素晴らしいだろう。

答えて

0

[OK]を、遅れて申し訳ありませんが、私は少し研究が必要でした。

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

は、私はあなたが必要な場合は、インポートする全体モナカIDEのプロジェクトを投稿することができます:モナカIDEとプラグインをインストールし、カスタムAndroidのビルドを行った後、私はそれが次のコードを使用して仕事を得ることができました。私は人々を助けてくれた他のスレッドのコードをたくさん含んでいました。注意すべき大きな点は、ons.readyを確認してから変数にアクセスする必要があることです。

あなたのArduinoプロジェクトにお役に立てば幸いです。

+0

ありがとうございました! – condo1234

+0

上記の例をOnsenuiコード(私の答えの下)に変える方法を知りたいのですが...モバイルアプリとのBluetooth接続はかなり普及していると思います。 – 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 
     } 
    }; 
関連する問題