Convert a characteristic value from BLE to byte array and convert into bytebuffer in objective c

Am new to objective c I need to set a characteristic data which I get from a Bluetooth using CB read to Byte array then return a byte buffer.

the same is written in java how can I do the same in objective c

dataArray = characteristic.getValue();
        tsLong = System.currentTimeMillis() - tsStart;
        float timeSeconds = (float) ((float) tsLong / 1000.0);
        String timerstring = String.format("%.2f", timeSeconds);
        message = convertByteToChannelData(ByteBuffer.wrap(dataArray), timerstring, 0);

this is the convertByteToChannelData method

public String convertByteToChannelData(ByteBuffer wrap,String timerString, int stimulus){
wrap.order(ByteOrder.LITTLE_ENDIAN);

        // Set 1 Channels
        // Channels 1, 5, 6, 11, 16, 17
        ch1R = (float) ((wrap.getShort(0) / 4095.) * 3.3);
        ch1Rs =  (float) ((wrap.getShort(4) / 4095.) * 3.3);
        ch5R =  (float) ((wrap.getShort(12) / 4095.) * 3.3);
        ch5Rs =  (float) ((wrap.getShort(4) / 4095.) * 3.3);
        ch6R =  (float) ((wrap.getShort(6) / 4095.) * 3.3);
}

It’s hard to answer this without knowing more about how Java works but I suspect that you have a byte buffer that contains 5 values — ch1R, ch1Rs, ch5R, ch5Rs, and ch6R — each one stored as little endian 16-bit integers. Is that right?

If so, the traditional approach in C-based languages is to create struct that matches the external representation:

struct Values {
    int16_t ch1R;
    int16_t ch1Rs;
    int16_t ch5R;
    int16_t ch5Rs;
    int16_t ch6R;
};
typedef struct Values Values;

__Check_Compile_Time(sizeof(Values) == 10);

and then work with that struct directly:

static Values valuesForData(NSData * data) {
    assert(data.length == sizeof(Values));
    Values v = *(const Values *) data.bytes;
    v.ch1R  = (int16_t) OSSwapLittleToHostInt16( (uint16_t) v.ch1R );
    v.ch1Rs = (int16_t) OSSwapLittleToHostInt16( (uint16_t) v.ch1Rs );
    v.ch5R  = (int16_t) OSSwapLittleToHostInt16( (uint16_t) v.ch5R );
    v.ch5Rs = (int16_t) OSSwapLittleToHostInt16( (uint16_t) v.ch5Rs );
    v.ch6R  = (int16_t) OSSwapLittleToHostInt16( (uint16_t) v.ch6R );
    return v;
}

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Convert a characteristic value from BLE to byte array and convert into bytebuffer in objective c
 
 
Q