2017-12-08 1 views
0

을 통해 Cordova iOS 프로젝트에서 중앙 블루투스를 초기화하려고합니다. 플러그인은 this github입니다. 내 코드는 다음과 같습니다디버그 모드에서 새로 고침하지 않으면 소스를 제대로로드 할 수 없습니다. Cordova iOS 프로젝트

-www

하는 index.js

var app = { 
    // Application Constructor 
    initialize: function() { 
     document.addEventListener('deviceready', this.onDeviceReady.bind(this), false); 
     //initialize the Bluetooth adapter 
     document.addEventListener('deviceready', function() { 
      new Promise(function (resolve) { 
       bluetoothle.initialize(resolve, { request: true, statusReceiver: false }); 
       }).then(initializeSuccess, handleError); 
      }); 
    }, 
/* rest part is just the Cordova helloword template*/ 
}; 

app.initialize(); 

/ServiceS/BLEService.js

function initializeSuccess(result) { 

    if (result.status === "enabled") { 

     log("Bluetooth is enabled."); 
     log(result); 
    } 

    else { 

     document.getElementById("start-scan").disabled = true; 

     log("Bluetooth is not enabled:", "status"); 
     log(result, "status"); 
    } 
} 

function handleError(error) { 

    var msg; 

    if (error.error && error.message) { 

     var errorItems = []; 

     if (error.service) { 

      errorItems.push("service: " + (uuids[error.service] || error.service)); 
     } 

     if (error.characteristic) { 

      errorItems.push("characteristic: " + (uuids[error.characteristic] || error.characteristic)); 
     } 

     msg = "Error on " + error.error + ": " + error.message + (errorItems.length && (" (" + errorItems.join(", ") + ")")); 
    } 

    else { 

     msg = error; 
    } 

    log(msg, "error"); 

    if (error.error === "read" && error.service && error.characteristic) { 

     reportValue(error.service, error.characteristic, "Error: " + error.message); 
    } 
} 

function log(msg, level) { 

    level = level || "log"; 

    if (typeof msg === "object") { 

     msg = JSON.stringify(msg, null, " "); 
    } 

    console.log(msg); 

    if (level === "status" || level === "error") { 

     var msgDiv = document.createElement("div"); 
     msgDiv.textContent = msg; 

     if (level === "error") { 

      msgDiv.style.color = "red"; 
     } 

     msgDiv.style.padding = "5px 0"; 
     msgDiv.style.borderBottom = "rgb(192,192,192) solid 1px"; 
     document.getElementById("output").appendChild(msgDiv); 
    } 
} 

및 index.html을

<html> 
    <head> 
     <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;"> 
     <meta name="format-detection" content="telephone=no"> 
     <meta name="msapplication-tap-highlight" content="no"> 
     <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> 
     <link rel="stylesheet" type="text/css" href="css/index.css"> 
     <title>Hello World</title> 
    </head> 
    <body> 

     <div class="app"> 
      <h1>Apache Cordova</h1> 
      <div id="deviceready" class="blink"> 
       <p class="event listening">Connecting to Device</p> 
       <p class="event received">Device is Ready</p>     
      </div> 
      <p id="output"></p> 
     </div> 
     <script type="text/javascript" src="cordova.js"></script> 
     <script type="text/javascript" src="js/Services/BLEService.js"></script> 
     <script type="text/javascript" src="js/index.js"></script> 
    </body> 
</html> 

근본적으로 여기의 모든 것은 ju 다 st는 시작 코드가 코르도바 helloword에서 얻는 것과 그 블루투스 플러그인의 github. 나는 XCode에서 프로젝트를 구축하려고 노력했고 모든 것이 지금까지 잘 작동한다. 기기에서 빌드하고 실행해도 오류가 발생하지 않습니다.

프로젝트를 휴대 전화에서 직접 실행 한 후 초기화 성공 여부에 관계없이 응답이 없습니다. xcode의 출력에서 ​​"Received Event : deviceready"라는 메시지가 표시됩니다. 그래서 나는 그것이 "약속"자바 스크립트의 일부라고 생각한다. 사파리에서 디버거를 링크하고 개발자 도구를 사용하십시오. 그 곳에서 수동으로 새로 고침을 클릭하면 다음 번에 응용 프로그램이 전화기에 다시로드 될 때 출력에서 ​​"Bluetooth 사용"및 { "상태": "사용"}을 볼 수 있습니다.

하지만 약속의 모든 기능이 처음으로로드되지 않는 이유는 없습니다. 아무도 그것에 대한 단서가 있습니까?

답변

0

좋아. 나는이 플러그인의 .m 부분을 실제로 이해하면서이 모든 것을 끝낸다. cordova-plugin-bluetoothle github에서 2017/12/11까지 BluetoothLePlugin.m 행 664를 살펴보면 처음으로 centralManager를 초기화 할 때 초기화 할 때 활성화 또는 비활성화 여부가 반환되지 않습니다. 오직 당신 만이 그것을 다시 불러 왔을 때 "활성화 된"피드백을 볼 수 있습니다. 따라서 index.js의 코드를 다음과 같이 변경했습니다.

document.addEventListener('deviceready', function() { 
      //the reason following function be called twice is, the plugin is designed like this 
      //(check BluetoothLePlugin.m line 664 and after) 
      //first time this function will intialize the centralManager, but will not check whether it is enabled or not 
      new Promise(function (resolve) { 
         bluetoothle.initialize(resolve, { request: true, statusReceiver: false }); 
         }).then(initializeSuccess, handleError); 
      //for the second time it will check if it's enabled or not 
      new Promise(function (resolve) { 
         bluetoothle.initialize(resolve, { request: true, statusReceiver: false }); 
         }).then(initializeSuccess, handleError); 
      }); 

그리고 예상 한대로 작동합니다. 필자는이 흥미로운 "기능"을 저자에게보고하여이 오해가 내 오해 또는 무엇 때문에 발생했는지 확인하려고합니다.

관련 문제