Search code examples
bluegiga

BGScript: Conversion of string into integer


I have an android app that sends data to BLE113 module. I receive data through a GATT characteristic of which type is 'user'. I am able to get the data as strings. When I send integers, say 24, I receive it as string '24'. Is there anyway I can convert this string number into integer type?

This is from gatt.xml.

<characteristic uuid="xxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxx" id="configuration">
    <description>Config Register</description>
    <properties read="true" write="true"/>
    <value type="user" />
</characteristic>

This is the snippet from Android side to write an integer value '1'.

String str = "1";
    try {
      byte[] value = str.getBytes("UTF-8");
      chara.setValue(value);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
.
.
.
boolean status = mBluetoothGatt.writeCharacteristic(chara);

I want to receive data as integer '1' itself in BGScript side. Am I doing anything wrong with conversion? Please help me to send integers.

Has it got to do anything with type 'USER' of GATT characteristic? If I change it to 'hex' or 'utf-8', will the issue be solved?


Solution

  • In the gatt.xml file, change the value type from "user" to "hex" and give it some fixed length, like so:

    <characteristic uuid="xxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxx" id="configuration">
        <description>Config Register</description>
        <properties read="true" write="true"/>
        <value type="hex">00</value>
    </characteristic>
    

    In your Android project, assuming chara is of type BluetoothGattCharacteristic, you write to the characteristic with something like:

    int newConfigRegValue = 1;
    chara.setValue(newConfigRegValue, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
    boolean status = mBluetoothGatt.writeCharacteristic(chara);
    

    Then, in your BGScript (I assume, you didn't say otherwise), in the attributes_value() event, you can save that integer like:

    dim configRegister
    ...
    event attributes_value(connection, reason, handle, offset, value_len, value_data)
    ...
        if handle = configuration then
            configRegister = value_data(0:value_len)    
        end if
    ...
    end
    

    -Ted

    -Begin Edit-

    You can do that too.

    The characteristic in your gatt.xml will look like this:

    <characteristic uuid="xxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxx" id="configuration">
        <description>Config Register</description>
        <properties read="true" write="true"/>
        <value type="user">0</value>
    </characteristic>
    

    Then in your BGScript file, you will need to provide a value when the read request comes in. This is done in the attributes_user_read_request() event. Like so:

    event attributes_user_read_request(connection, handle, offset, maxsize)
        if handle = configuration then
            # Do something to retreive the configRegister value
            # If you need to read from external EEPROM or something, save the 'connection' value so you can use it in the callback event
            call attributes_user_read_response(connection, 0, 1, configRegister(0:1))
        end if
    end