Jewelbots / ng-cordova-bluetoothle

Angular 1.x wrapper for the PhoneGap/Cordova Bluetooth Low Energy Plugin
Other
14 stars 43 forks source link

I got error to read data from ble MLDPTerminal #9

Open konda01 opened 7 years ago

konda01 commented 7 years ago

I got following error. "error":"read","message":"Unable to read on return"

I am using cordova bluetoothle plugin.

gortok commented 7 years ago

You're going To have to include your code that is causing this error and where in the code it happens. It will also help to know what device you're talking to.

konda01 commented 7 years ago

The following code for used read data from RN4020 Bluetooth Low Energy (Microchip ).

/***************************************************************
   Read data from connected device.
*****************************************************************/
var readData = function() {

      var params = {
        address:'XX:XX:XX:XX',
        service: 'XXX-XXXX-XXXX',
        characteristic: 'XXX-XXXX-XXXX',
        timeout: 5000
      };

     console.log("Read : " + JSON.stringify(params));
     bluetoothle.read(readSuccess, readError, params);

};
var readSuccess= function(obj){
        console.log("Read Success : " + JSON.stringify(obj));
        if (obj.value && (!obj.type || obj.type == "data")) {
            var bytes = bluetoothle.encodedStringToBytes(obj.value);
            console.log("ASCII (" + bytes.length + "): " + bluetoothle.bytesToString(bytes));
            console.log("HEX (" + bytes.length + "): " + bluetoothle.bytesToHex(bytes));
        }

}
var readError = function(obj){
   console.log("Read Error : " + JSON.stringify(obj));
}
konda01 commented 7 years ago

But I got the following error. "error":"read","message":"Unable to read on return".

And also i got the following warning.

THREAD WARNING: exec() call to BluetoothLePlugin.read blocked the main thread for 19ms. Plugin should use CordovaInterface.getThreadPool().

gortok commented 7 years ago

So it looks like you're not even using the wrapper; but another issue is that for you to read, you have to do service discovery first; Here's an example that uses the wrapper:

https://github.com/gortok/jewelapp/blob/master/app/scripts/device/jewelbot-service.js#L28

konda01 commented 7 years ago

Hi Gortok, I did write the Bluetooth initialize, scan and connect after discovery the device then, I try to read the data that time i have problem

/* Bluetooth initialize checking. ***/ var initialize = function() { var params = { request: true, //restoreKey: "bluetooth-test-app" };

        console.log("Initialize : " + JSON.stringify(params));
        bluetoothle.initialize(initializeResult,params);
  };

  function initializeResult(obj){
    console.log("Initialize Success : " + JSON.stringify(obj));
    startScan();
  }

/* Scan Device ***/ var startScan = function() { var params = { services:[], allowDuplicates: false, scanTimeout: 5, };

    if (window.cordova) {
      params.scanMode = bluetoothle.SCAN_MODE_LOW_POWER;
      params.matchMode = bluetoothle.MATCH_MODE_STICKY;
      params.matchNum = bluetoothle.MATCH_NUM_ONE_ADVERTISEMENT;
      //params.callbackType = bluetoothle.CALLBACK_TYPE_FIRST_MATCH;
    }

    console.log("Start Scan : " + JSON.stringify(params));
    $('.blelist').empty();
    bluetoothle.startScan(startScanSuccess, startScanError, params);

};
var startScanSuccess = function(obj){
    console.log("Start Scan Auto Stop : " + JSON.stringify(obj));
    $('.blelist').append("<li><a href='#' onclick='connect(\""+obj.address+"\");'>"+obj.name+"</a></li>");
    $(document).trigger('create');
    $(".blelist").listview('refresh');
}
var startScanError = function(obj){
    console.log("Start Scan Auto Stop : " + JSON.stringify(obj));
}

/* Connect code ***/ var connect = function(address) { alert(address); var params = {address:address, timeout: 10000};

    console.log("Connect : " + JSON.stringify(params));
    bluetoothle.connect(connectSuccess, connectError, params);

  };
  var connectSuccess = function(obj){
     console.log("Connect Success : " + JSON.stringify(obj));
     alert("Connected");
     if( platformName== 'Android' ){
        //alert("android");
        discover();
     }else{
        //alert("ios");
        characteristics();
        descriptors();

     }

  }
  var connectError = function(obj){
    console.log("Connect Error : " + JSON.stringify(obj));
  }

/* Discover data. ***/ var discover = function() { var params = { address: 'XXXXXXXXXX', // "clearCache": true, timeout: 10000 };

    console.log("Discover : " + JSON.stringify(params));
    bluetoothle.discover(discoverSuccess, discoverError, params);

};

var discoverSuccess = function(obj){
    console.log("Discover Success : " + JSON.stringify(obj));
}
var discoverError = function(obj){
    console.log("Discover Error : " + JSON.stringify(obj));
}
gortok commented 7 years ago

So where does it error now and what is the error?

konda01 commented 7 years ago

When i was read the data from ble device i got error. I got following error. "error":"read","message":"Unable to read on return"