need info
need info

Reputation: 21

randdusing/ng-cordova-bluetoothle, parsing ble advertisement ionic

using the randdusing bluetoothle plugin for ionic app, need to read the advertisement.

The ble scan returns with Start Scan Success :

{"address":"14::30:c6:60:e8;9f","name":null,"rssi":-50,"advertisement":"AgEGG/9SVgIADSw5YTNlMTQAAAJlAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA;=","status":"scanResult"}

query: need to decipher this json data and convert this advertisement data into array containing hex values of advertisement data? The advertisement data seems to be base64 encoded. Please advice.

Upvotes: 1

Views: 649

Answers (1)

Tobias Philipp
Tobias Philipp

Reputation: 93

I made for this purpose a little helper function as shown below. The key is the $cordovaBluetoothLE.encodedStringToBytes as you can see in docs https://github.com/randdusing/ng-cordova-bluetoothle.

var encodedToByteString = function encodedToByteString(input) {
    var val = $cordovaBluetoothLE.encodedStringToBytes(input);
    var byteStr = "";
    for (var i = 0; i < val.length; i++) {
        var byte = val[i].toString(16);
        if (byte.length == 1) byte = "0" + byte;
        byteStr += byte;
    }
    return byteStr;
};

The same goes for the opposite operation - that is sending data. You first need to get your hex-string into an array of bytes and then encode it via $cordovaBluetoothLE.bytesToEncodedString(value).

Upvotes: 1

Related Questions