Page 1 of 1

Write to a ble unit

Posted: 13:12, 19 Jan 2016
by robbal
Hi Guys


I am not sure how to write this but I will try. Please bear with me as I am a newbie at JavaScript.

I have a 16byte (69C7E98730B2472CB8B5DE17A703C3F0) value that I need to write to BLE unit(clients unit).

I connect to the BLE unit , read the service and get the 16byte value , which I need to write back to the same service. I am not sure how to write the value back. I have tried the following and it does not seem to work and the crappy part is that the unit has no feedback to tell me.

I do know that I am reading the value right, so that's at least a starting point. Here is an example of my code. I would really appreciated it if anyone has some insight into this.

Code: Select all

$scope.writePassword = function(device, resultCode){

  data=new Uint8Array(resultCode.length);
  for(var i=0,j=resultCode.length;i<j;++i){
     data[i]=resultCode.charCodeAt(i);
  }

  device.writeCharacteristic(
        '69C7E987-30B2-472C-B8B5-DE17A703C3F0',
        data.buffer,
        function(data){
            console.log("TAG writeCharacteristic is ok");

            evothings.easyble.closeConnectedDevices(device);
            $scope.scanForUnits();

        },
        function(error){
            console.log('TAG Error: writeCharacteristic failed ' + error);
        });
};




Thank you in advance

Robby

Re: Write to a ble unit

Posted: 20:29, 19 Jan 2016
by Fredrik
I'll try to help, but I'm a bit confused. The value you want to write seems to be identical to the characteristic's UUID. Is that correct?

The function you've written takes the parameter "resultCode", assumes it is a string, then writes the ASCII representation of that string to the characteristic. Is that right?

When you run the function, do you get any console.log output? One would hope for either "TAG writeCharacteristic is ok" or "TAG Error".

Re: Write to a ble unit

Posted: 07:09, 20 Jan 2016
by robbal
Hi Fredrik

Thank you for the help ... I just took that value as an example. Here is an exact value 0b5ac7b1206a15531553038715530187.

Yes you are right, I am trying to write as ASCII representation. The console.log reports "Error: writeCharacteristic failed" and then the evothing plugin keeps trying to connect again with the same results. I first used the cordova-plugin-ble-central plugin and this worked for that plugin, but I changed because I liked Evothing more. I assumed it would be the same.

Thanks again

Robby

Re: Write to a ble unit

Posted: 07:17, 20 Jan 2016
by Fredrik
OK, so if you have the javascript string "0b5ac7b1206a15531553038715530187", then that's too long for BLE writing, because there's a limit of 20 bytes per characteristic.

Perhaps you should treat it as hex bytes, like so:

data = new Uint8Array([0x0b, 0x5a, 0xc7, ...])

Re: Write to a ble unit

Posted: 07:34, 20 Jan 2016
by robbal
ok .. i see what you are saying, Going to try it and will revert

Thanks

Re: Write to a ble unit

Posted: 08:13, 20 Jan 2016
by robbal
WOW! That didnt hang the ble unit.... so progress. I must just work out how to take a string and convert it to that array... Will keep you posted.....
;)

Thanks so far

Re: Write to a ble unit

Posted: 18:47, 24 Jan 2016
by robbal
Thank you all.. so much

I landed up with this

var data = new Uint8Array(s.match(/../g).map(function (s) {
return parseInt(s, 16);
}));