0

In my Android app I receive data from my ble device as something like 10100201201511 ( it includes date,time and other attributes).

I hope I received it as a string, I want to check that value and display it on textview.

My Requirement is

If (position1=0){
  //display yes in Text view
}else
{ // display No in Textview 
}  

But I always get errors like Array type expected; found 'Java.lang.String'

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);


                    if (names[0]=0){
                        line1.setText("got it");
                    }
                    else{
                        line1.setText("Nil");
                    }

Plz help me to resolve this..

Mr Robot
  • 1,747
  • 6
  • 35
  • 67
  • 1
    Why do you try to convert you string to a byte array and again to a string ? Can't you just use `names` ? It's already a String, isn't it ? – singe3 Jul 17 '15 at 07:54
  • yes . i removed that and try to run like u said. but the error shows .array required,But String found. – Mr Robot Jul 17 '15 at 08:00
  • 1
    I've never worked with bluetooth myself, but an obvious mistake is that you're trying to use `names` as an `Array`. That is not possible. Aside from that, if you want to compare 2 Strings, you should do it like `"0".equals(someString)` – SnyersK Jul 17 '15 at 08:21
  • Okay Leave about Bluetooth.. Can we convert this "names" to array. – Mr Robot Jul 17 '15 at 09:10

1 Answers1

2

I assume the error occurs at names[0]=0 because names is a string.

If you want to read the first letter of the string use

   if ( !names.isEmpty() && names.substring(0,1).equals("0"))
         line1.setText("got it");
   else
         line1.setText("Nil");
singe3
  • 2,065
  • 4
  • 30
  • 48
  • This code is partially working for me now. but it always shows the else part. – Mr Robot Jul 17 '15 at 09:43
  • 1
    @RubinNellikunnathu Then that's because your string does not contains "0" as the first character. Add a print statement for debugging to see what is your string. – singe3 Jul 17 '15 at 09:55
  • It Worked for me now.. Brother . I had one more doubt. how will i set a substring value to a textview. like line1.setText(???). For Example ,I am receiving date and time in that string. i need to show date and time separately in different textviews – Mr Robot Jul 17 '15 at 10:58
  • calling the substring method on a string return a new string of the size chosen in the parameters. If you need to have two substrings, use `String a = names.substring(x,y);` and `String b = names.substring(x', y');` then pass your string to your TextView objects (replacing x, y, x' and y' by the values you need, check the javadoc of substring http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)) – singe3 Jul 17 '15 at 13:56