плагин cordova BluetoothLE

#android #cordova #bluetooth #cordova-plugins

#Android #кордова #bluetooth #cordova-плагины

Вопрос:

У меня возникли некоторые проблемы с использованием подключаемого модуля BluetoothLE для apache cordova. Это первый раз, когда я пытаюсь его установить, так что, возможно, я чего-то не хватает.

В моем репозитории: плагин $ cordova для com.randdusing.bluetoothle 1.0.0 «Bluetooth LE»

Таким образом, плагин должен быть установлен. И вот мой js :

 $(document).ready(function(){
    //try to enable bluetooth
    bluetoothle.initialize(initializeSuccess, initializeError, true);
    $("#tiles").append($('Bye'));
});

function initializeSuccess(obj) {
    $("#tiles").append($('Hello'));
}

function initializeError(obj) {
    $("#tiles").append($('Hello'));
}
  

Итак, у меня должно быть на странице моего приложения «Привет», за которым следует «Пока», но это не работает. Я не касался ни одного файла, сгенерированного cordova, кроме папки www.
Чего мне не хватает?

Ответ №1:

Хорошо, у меня это работает. Вот полный код :

index.html :

 <html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <meta name="msapplication-tap-highlight" content="no" />
        <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>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>
</html>
  

index.js :

 var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        alert("Hello");
        bluetoothle.initialize(initializeSuccess, initializeError, true);
        alert("Bye");
    }
};

function initializeSuccess(obj) {
    alert("Success");
}

function initializeError(obj) {
    alert("Failure");
}