Phonegap app
Bluetooth Plugin: cordova-plugin-ble-central
https://github.com/don/cordova-plugin-ble-central
Using Javascript and HTML.
I am currently writing a PhoneGap app which is reading, writing and getting notifications to and from a bluetooth le device. It's a JavaScript API to the bluetooth device which can be used by the main application. Almost everything is working well and I am using the pattern below for reading/writing which successfully reads and write to services on the bluetooth device.
intervalSet: function(deviceId, loggerIntervalSeconds, callback) {
/* 'interval' below is computed from incoming seconds to be expressed
as an integer for either minutes or seconds
(whole number only allowed)
*/
var loggerIntervalArray = new Uint8Array(1);
loggerIntervalArray[0] = interval;
var onLoggerInterval = function() {
callback({"Interval": interval});
}
ble.write(deviceId, service.logger.serviceId, service.logger.loggerIntervalChar, loggerIntervalArray.buffer, onLoggerInterval, btcomms.onServiceConnectionError);
},
One of the service characteristics, a logger, stores it's data in Packed BCD format. Although I understand the format and can visualize how that looks in the device - I can't figure how to get my app to write the data in the correct format. It's a time based characteristic and I am trying to write an interval to it. Here is the specification for the service, from the engineer:
To set an interval value
Read/write Length = 1
To set 10 minutes: 0x10
To set 1 hour: <0x81>
Logging interval in hours or minutes.
If MSB is set then the value is in HOURS;
otherwise it’s MINUTES.
Packed BCD format.
The incoming value from the UI is the number of seconds in the required interval. I convert that to either minutes or hours in integer format and I need to ready the data for writing to the characteristic, and the ble device reads anything over 128 as hours, i.e. 1 = 1 minute, 10000001 = 1 hour (hence 0x81 for one hour)
I have been trying various things, e.g.:
val >>>0).toString(4)
var.toString(16)
etc.
but to no avail. I'd be interested if anyone has had to do this before and if they could explain how to 'prepare' my integers for writing to the engineer's bluetooth board...
by the way, the plugin function ble.write() expects: (device id, service id, interval characteristic id, the data, success, error)
I hope there's enough information there. Any help is appreciated.
Mike