Search code examples
androidandroid-bluetoothandroid-ble

How to extract a bit from a byte and display it on a textview, in Android?


I am receiving a string data from a ble device. I stored it in String names I need to extract a byte from its eighth position, then I need to extract all the bits from the byte.

How can I achieve this?

Now, I am only able to receive the string . and im able to extract the first 2 sub strings.

My current code is :

@Override
public void onCharacteristicRead(BluetoothGatt gatt,
                                 BluetoothGattCharacteristic characteristic, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        if (BleUuid.READ_TIME
                .equalsIgnoreCase(characteristic.getUuid().toString())) {
            final String names = characteristic.getStringValue(0);


            runOnUiThread(new Runnable() {
                public void run() {
                    statuslv.setText("Status Received..");
                    setProgressBarIndeterminateVisibility(false);
                    if ( !names.isEmpty() && names.substring(0,1).equals("a")){
                        line1.setText("got it");
                    }
                    else{
                        line1.setText("Nil");
                    }
                    if ( !names.isEmpty() && names.substring(1,2).equals("a")){
                        line2.setText("got it again");
                    }
                    else{
                        line2.setText("Nil again");
                    }


                }
            });

Solution

  • Use String.getBytes( charsetName ) if you want to extract the bytes of your String.

    Please post relevant code.