bcsphere / bluetooth

Please support! Kindly "Star" this standard Bluetooth Smart API / SDK :) The "bcsphere/bluetooth" is a phonegap/cordova plugin. It's a Bluetooth JavaScript API from JUMA, a Bluetooth expert team. It supports both Bluetooth 4.0 GATT/Bluetooth Low Energy (BLE) interface in iOS/Android and Bluetooth 2.1 Classic Rfcomm / L2cap socket interface as Bluetooth Serial Port/SPP, as well as iBeacon!
Apache License 2.0
298 stars 12 forks source link

bcready never fires #42

Open BenjaminHorn opened 9 years ago

BenjaminHorn commented 9 years ago

I made a new phonegap project, and added your plugin.

phonegap add https://github.com/bcsphere/bluetooth.git 

In index.html I added

<script src="bc.js"></script>

Do I need other files?

My index.js looks like this:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
   onBCReady: function() {
        console.log('bcready');
    },    
    // 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) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
        document.addEventListener('bcready', app.onBCReady, false);
    }
};

I can see the result of console.log('Received Event: ' + id); but never 'bcready'.

Phonegap 5.1.1-0.29.0 Android 4.3

lizhijumacc commented 9 years ago

try this :

    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        document.addEventListener('bcready', this.onBCready, false);
    }

may be the 'bcready' event has been fired before the 'receivedEvent' be called.

I'll try it later.

BenjaminHorn commented 9 years ago

@lizhijumacc thanks for your reply! Unfortunately I can still see only

[phonegap] [console.log] Received Event: deviceready

After some remote debug it turned out, BC or window.BC is null.

Looks like

<script src="bc.js"></script>

does not load BC namespace.

BenjaminHorn commented 9 years ago

Is any news on this?