11 Replies - 581 Views - Last Post: 04 August 2019 - 08:59 AM

#1 Alexandros81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-May 19

Developing a Bluetooth app. It crashes when setText is used. Android

Posted 19 May 2019 - 10:59 AM

Hi!
I am using a HC-05 Bluetooth Module with a micro-controller.
The micro-controller sends data via UART to HC-05 and I can see the data
to my mobile phone using an app that I have copied from here:
http://www.electroni...-via-bluetooth/

Now I have made some changes to the android app code and added two textView(s)
and two EditText(s).

The code follows:

package com.example.fishingboattestapp;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends Activity {
    //    private final String DEVICE_NAME="MyBTBee";
    private final String DEVICE_ADDRESS="98:D3:31:F5:BD:D8";
    private final UUID PORT_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");//Serial Port Service ID
    private BluetoothDevice device;
    private BluetoothSocket socket;
    private OutputStream outputStream;
    private InputStream inputStream;
    Button startButton, sendButton,clearButton,stopButton;
    TextView textView;
    EditText editText;
    TextView textView2;
    EditText editText2;
    TextView textView3;
    EditText editText3;
    boolean deviceConnected=false;
    Thread thread;
    byte buffer[];
    int bufferPosition;
    boolean stopThread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startButton = (Button) findViewById(R.id.buttonstart);
        sendButton = (Button) findViewById(R.id.buttonSend);
        clearButton = (Button) findViewById(R.id.buttonClear);
        stopButton = (Button) findViewById(R.id.buttonstop);
        editText = (EditText) findViewById(R.id.editText);
        textView = (TextView) findViewById(R.id.textView);
        editText2 = (EditText) findViewById(R.id.editText2);
        textView2 = (TextView) findViewById(R.id.textView2);
        editText3 = (EditText) findViewById(R.id.editText3);
        textView3 = (TextView) findViewById(R.id.textView3);
        setUiEnabled(false);

    }

    public void setUiEnabled(boolean bool)
    {
        startButton.setEnabled(!bool);
        sendButton.setEnabled(bool);
        stopButton.setEnabled(bool);
        textView.setEnabled(bool);

    }

    public boolean BTinit()
    {
        boolean found=false;
        BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show();
        }
        if(!bluetoothAdapter.isEnabled())
        {
            Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableAdapter, 0);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
        if(bondedDevices.isEmpty())
        {
            Toast.makeText(getApplicationContext(),"Please Pair the Device first",Toast.LENGTH_SHORT).show();
        }
        else
        {
            for (BluetoothDevice iterator : bondedDevices)
            {
                if(iterator.getAddress().equals(DEVICE_ADDRESS))
                {
                    device=iterator;
                    found=true;
                    break;
                }
            }
        }
        return found;
    }

    public boolean BTconnect()
    {
        boolean connected=true;
        try {
            socket = device.createRfcommSocketToServiceRecord(PORT_UUID);
            socket.connect();
        } catch (IOException e) {
            e.printStackTrace();
            connected=false;
        }
        if(connected)
        {
            try {
                outputStream=socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                inputStream=socket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        return connected;
    }

    public void onclickStart(View view) {
        if(BTinit())
        {
            if(BTconnect())
            {
                setUiEnabled(true);
                deviceConnected=true;
                beginListenForData();
                textView.append("\nConnection Opened!\n");
            }

        }
    }

    void beginListenForData()
    {
        final Handler handler = new Handler();
        stopThread = false;
        buffer = new byte[1024];
        Thread thread  = new Thread(new Runnable()
        {
            public void run()
            {
                while(!Thread.currentThread().isInterrupted() && !stopThread)
                {
                    try
                    {
                        int byteCount = inputStream.available();
                        if(byteCount > 0)
                        {
                            byte[] rawBytes = new byte[byteCount];
                            inputStream.read(rawBytes);
                            final String string=new String(rawBytes,"UTF-8");
                            handler.post(new Runnable() {
                                public void run()
                                {
                                    textView.append(string);
                                    String[] data1 = string.split("#");
                                    CharSequence cs = data1[2];
                                    editText2.setText(cs, TextView.BufferType.EDITABLE);
                                }
                            });

                        }
                    }
                    catch (IOException ex)
                    {
                        stopThread = true;
                    }
                }
            }
        });

        thread.start();
    }

    public void onclickSend(View view) {
        String string = editText.getText().toString();
        string.concat("\n");
        try {
            outputStream.write(string.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        textView.append("\nSent Data:"+string+"\n");

    }

    public void onclickStop(View view) throws IOException {
        stopThread = true;
        outputStream.close();
        inputStream.close();
        socket.close();
        setUiEnabled(false);
        deviceConnected=false;
        textView.append("\nConnection Closed!\n");
    }

    public void onclickClear(View view) {
        textView.setText("");
    }
}





<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginStart="25dp"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="13dp"
        android:layout_marginEnd="103dp"
        android:layout_marginRight="103dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Begin"
        android:id="@+id/buttonstart"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onclick="onclickStart"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/buttonSend"
        android:onclick="onclickSend"
        android:layout_below="@+id/editText"
        android:layout_toRightOf="@+id/buttonstart"
        android:layout_toEndOf="@+id/buttonstart" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="395dp"
        android:layout_height="120dp"
        android:layout_below="@+id/buttonSend"
        android:layout_alignEnd="@+id/editText"
        android:layout_alignRight="@+id/editText"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginEnd="-16dp"
        android:layout_marginRight="-16dp"
        android:singleLine="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop"
        android:id="@+id/buttonstop"
        android:layout_below="@+id/editText"
        android:layout_toRightOf="@+id/buttonSend"
        android:layout_toEndOf="@+id/buttonSend"
        android:onclick="onclickStop"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear"
        android:id="@+id/buttonClear"
        android:layout_below="@+id/editText"
        android:layout_toRightOf="@+id/buttonstop"
        android:layout_toEndOf="@+id/buttonstop"
        android:onclick="onclickClear"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="60dp"
        android:text="Speed:"
        android:textColor="@color/design_default_color_primary_dark"
        android:textSize="24sp"/>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="167dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="-12dp"
        android:layout_marginEnd="90dp"
        android:layout_marginRight="90dp"
        android:layout_marginBottom="-4dp"
        android:layout_toEndOf="@+id/textView2"
        android:layout_toRightOf="@+id/textView2"
        android:background="@drawable/custom_edit_text_bg" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="36dp"
        android:text="Distance:"
        android:textColor="@color/design_default_color_primary_dark"
        android:textSize="24sp"/>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="168dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginStart="21dp"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="-4dp"
        android:layout_marginEnd="91dp"
        android:layout_marginRight="91dp"
        android:layout_marginBottom="-10dp"
        android:layout_toEndOf="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:background="@drawable/custom_edit_text_bg" />
</RelativeLayout>




In the following part of the code

public void run()
  {
        textView.append(string);
        String[] data1 = string.split("#");
        CharSequence cs = data1[2];
        editText2.setText(cs, TextView.BufferType.EDITABLE);
  }




the line
editText2.setText(cs, TextView.BufferType.EDITABLE); 

crashes the app.

The data I receive there is the '#' symbol in between them.
So I separate them and place them to string array.
I want to display data1[2] string to editText2.

Any idea what is causing the app to crash and how to fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: Developing a Bluetooth app. It crashes when setText is used. Android

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12657
  • View blog
  • Posts: 45,830
  • Joined: 27-December 08

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 19 May 2019 - 12:08 PM

If your app is crashing, there should be an error message on the LogCat window of your IDE. Please post the entire error message, exactly as it appears.
Was This Post Helpful? 0
  • +
  • -

#3 Alexandros81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-May 19

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 19 May 2019 - 12:31 PM

Hi.
The way I see the app crashing is by connecting my mobile
phone (Samsung A3) to my pc and press the play button after
building. Then I choose my mobile phone as the deployment device
and press ok. So finaly the app runs in my mobile phone.
When app is active and I press the BEGIN button after a couple of
seconds the app shuts down.

Ah ok. Here is the LogCat error message:

05-19 22:14:48.196 26492-26492/com.example.fishingboattestapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fishingboattestapp, PID: 26492
java.lang.ArrayIndexOutOfBoundsException: length=1; index=2
at com.example.fishingboattestapp.MainActivity$1$1.run(MainActivity.java:172)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)


I hope this helps!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12657
  • View blog
  • Posts: 45,830
  • Joined: 27-December 08

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 19 May 2019 - 03:10 PM

The error occurs at this line: CharSequence cs = data1[2];. The error message is telling you that there is only a single element in the array. It seems you are making assumptions about the format of your String. I would check more carefully the String you are getting leading up to line 172.

Quote

java.lang.ArrayIndexOutOfBoundsException: length=1; index=2

Was This Post Helpful? 0
  • +
  • -

#5 Alexandros81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-May 19

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 20 May 2019 - 06:48 AM

@macosxnerd101

Thanks for your reply.
Let me explain the format of the received message from my mobile phone.
The data received is in the format:
LLLL.LLLLL#LLLLL.LLLLL#S.SSS#DD.DD
This was checked by a mobile app called "Bluetooth Controller".

Where LLLL.LLLLL is Latittude,
LLLLL.LLLLL is Longitude,
S.SSS is Speed Over Ground,
DD.DD is Course Over Ground.

This message is received from the mobile phone every 2 seconds.

That's why I use the line
String[] data1 = string.split("#");

to separate the data and place them to a string array.

LLLL.LLLLL (Latittude) should be placed in data1[0]
LLLLL.LLLLL (Longitude) should be placed in data1[1]
S.SSS (Speed Over Ground) should be placed in data1[2]
DD.DD (Course Over Ground) should be placed in data1[3]

Quote

It seems you are making assumptions about the format of your String. I would check more carefully the String you are getting leading up to line 172.


Any suggestions how to do that? or how to proceed next?

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12657
  • View blog
  • Posts: 45,830
  • Joined: 27-December 08

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 20 May 2019 - 07:15 AM

I would print the variable string to the logger, so you can see the text that is actually present. Similarly, log the string after you finish reading in the data and at various points as you modify it (up to line 172).

Based on the error message, the array consists only of this piece.

Quote

LLLL.LLLLL (Latittude) should be placed in data1[0]

Was This Post Helpful? 0
  • +
  • -

#7 Alexandros81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-May 19

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 20 May 2019 - 08:39 AM

Quote

I would print the variable string to the logger, so you can see the text that is actually present. Similarly, log the string after you finish reading in the data and at various points as you modify it (up to line 172).


@macosxnerd101

I placed this line in code:
handler.post(new Runnable() {
  public void run()
  {
      textView.append(string);
      [b]Log.d(TAG,string);[/b]
  }
});



And this is what I get in LOGCAT:

05-20 18:11:07.573 15128-15128/com.example.fishingboattestapp D/Logging Example: L
05-20 18:11:07.623 15128-15128/com.example.fishingboattestapp D/Logging Example: LLL.4548
05-20 18:11:07.663 15128-15128/com.example.fishingboattestapp D/Logging Example: 1#LLLLL.2
05-20 18:11:07.683 15128-15128/com.example.fishingboattestapp D/Logging Example: 5194#0.216
05-20 18:11:07.693 15128-15128/com.example.fishingboattestapp D/Logging Example: ��������#342.
05-20 18:11:07.713 15128-15128/com.example.fishingboattestapp D/Logging Example: 29������

Where L is a number that I hide. (cause Lat and Long are of my current location)
What does the question mark mean?
Was This Post Helpful? 0
  • +
  • -

#8 ben255   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 528
  • Joined: 09-September 13

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 20 May 2019 - 09:10 AM

cool i have the same bluetooth component :D But i guess when youre splitting data1 you get a array of 1 and then you try to read on position three which is out of bounds(it goes 0, 1, 2). The first string has no '#' and second too, so will it not just create one position array in that case? since there is nothing to split

the question marks are chars it cannot recognize
Was This Post Helpful? 1
  • +
  • -

#9 Alexandros81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-May 19

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 20 May 2019 - 09:46 AM

@ ben255

Are you telling me that each logcat message I attahed in my previous
message is a string?

For example in this log message
05-20 18:11:07.573 15128-15128/com.example.fishingboattestapp D/Logging Example: L

'L' is perceived as string?

and then 'LLL.4548' is perceived as a string too?

If yes then can I receive 1 string every 2sec in the form:
LLLL.LLLLL#LLLLL.LLLLL#S.SSS#DD.DD

and then split it?

or how do I go about splitting the data in
the form I want?
Was This Post Helpful? 0
  • +
  • -

#10 ben255   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 528
  • Joined: 09-September 13

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 20 May 2019 - 11:25 AM

Log.d doesnt allow integers if you dont do a toString() after so yes its a string. If you get the data in the way youre describing i would just use a for loop from 0 to string.length iterate all chars when you hit the '#' take out 0 to x then save that position in x value and continue until you hit the next '#' save that position until you hit the length. then save the pieces in a string array.
then you always know the length of the array and where each piece of data is
Was This Post Helpful? 2
  • +
  • -

#11 Alexandros81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-May 19

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 04 August 2019 - 07:40 AM

Hi.
It has been a while since I wrote on this thread.
My issue is not solved yet.

Here is my code:

void beginListenForData()
    {
        final Handler handler = new Handler();
        stopThread = false;
        buffer = new byte[1024];

        Thread thread  = new Thread(new Runnable()
        {
            public void run()
            {
                while(!Thread.currentThread().isInterrupted() && !stopThread)
                {
                    try
                    {
                        int byteCount = inputStream.available();
                        if(byteCount > 0)
                        {
                            int bytes; // bytes returned from read()

                            //byte[] rawBytes = new byte[byteCount];
                            //inputStream.read(rawBytes);
                            //final String string=new String(rawBytes,"UTF-8");
                            DataInputStream mmInStream = new DataInputStream(inputStream);
                            DataOutputStream mmOutStream = new DataOutputStream(outputStream);

                            // Read from the InputStream
                            bytes = mmInStream.read(buffer);
                            final String readMessage = new String(buffer, 0, bytes);

                            handler.post(new Runnable() {
                                public void run()
                                {
                                    String latitude = "";
                                    String longitude = "";
                                    int flag1 = 0;
                                    textView.append(readMessage);
                                    Log.d(TAG,readMessage);

                                    if(String.valueOf(readMessage.charAt(0)) == "A"){
                                        flag1 = 1;

                                    }

                                    if((flag1 == 1) && (String.valueOf(readMessage.charAt(0)) != "A")){
                                        latitude =  readMessage;
                                        flag1 = 2;
                                    }

                                    if((flag1 == 2) && (String.valueOf(readMessage.charAt(0)) != "A")){
                                        latitude = latitude + (String.valueOf(readMessage.charAt(0)));
                                        longitude = (String.valueOf(readMessage.charAt(2)));
                                        longitude = longitude + (String.valueOf(readMessage.charAt(3)));
                                        longitude = longitude + (String.valueOf(readMessage.charAt(4)));
                                        longitude = longitude + (String.valueOf(readMessage.charAt(5)));
                                        longitude = longitude + (String.valueOf(readMessage.charAt(6)));
                                        longitude = longitude + (String.valueOf(readMessage.charAt(7)));
                                        longitude = longitude + (String.valueOf(readMessage.charAt(8)));
                                        flag1 = 3;
                                    }

                                    if((flag1 == 3) && (String.valueOf(readMessage.charAt(0)) != "A")){
                                        longitude = longitude + (String.valueOf(readMessage.charAt(0)));
                                        longitude = longitude + (String.valueOf(readMessage.charAt(1)));
                                        longitude = longitude + (String.valueOf(readMessage.charAt(2)));
                                        longitude = longitude + (String.valueOf(readMessage.charAt(3)));
                                        flag1 = 0;

                                        Log.d(TAG,latitude);
                                        Log.d(TAG,longitude);
                                    }


                                    /*for(int i=0; i==(readMessage.length()); i++){
                                        string = string + readMessage;
                                    }
                                    String[] data1 = readMessage.split("#");*/

                                    //CharSequence cs = data1[2];
                                    //editText2.setText(cs, TextView.BufferType.EDITABLE);
                                    //char c = data1[0].charAt(0);
                                    //editText2.setText(data1[2]);
                                    //if (c == 'C') {
                                        //editText2.setText(data1[0], TextView.BufferType.EDITABLE);
                                    //}
                                }
                            });

                        }
                    }
                    catch (IOException ex)
                    {
                        stopThread = true;
                    }
                }
            }
        });

        thread.start();
    }



The data now come in the form:

A
LLLL.LLLL
L#lllll.l
llll#S.S
SS????????#DDD
.DD??????

I don't get the LAtitude and Longitude as I would like with my code above.

View Postben255, on 20 May 2019 - 11:25 AM, said:

Log.d doesnt allow integers if you dont do a toString() after so yes its a string. If you get the data in the way youre describing i would just use a for loop from 0 to string.length iterate all chars when you hit the '#' take out 0 to x then save that position in x value and continue until you hit the next '#' save that position until you hit the length. then save the pieces in a string array.
then you always know the length of the array and where each piece of data is


Hi!
Is it possible to give me some code?
Was This Post Helpful? 0
  • +
  • -

#12 Alexandros81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-May 19

Re: Developing a Bluetooth app. It crashes when setText is used. Android

Posted 04 August 2019 - 08:59 AM

Hi!
I have also tried this:

void beginListenForData()
    {
        final Handler handler = new Handler();
        stopThread = false;
        buffer = new byte[1024];

        Thread thread  = new Thread(new Runnable()
        {
            public void run()
            {
                while(!Thread.currentThread().isInterrupted() && !stopThread)
                {
                    try
                    {
                        int byteCount = inputStream.available();
                        if(byteCount > 0)
                        {
                            int bytes; // bytes returned from read()

                            //byte[] rawBytes = new byte[byteCount];
                            //inputStream.read(rawBytes);
                            //final String string=new String(rawBytes,"UTF-8");
                            DataInputStream mmInStream = new DataInputStream(inputStream);
                            DataOutputStream mmOutStream = new DataOutputStream(outputStream);

                            // Read from the InputStream
                            bytes = mmInStream.read(buffer);
                            final String readMessage = new String(buffer, 0, bytes);

                            handler.post(new Runnable() {
                                public void run()
                                {
                                    String latitude = "";
                                    String longitude = "";
                                    int flag1 = 0;
                                    textView.append(readMessage);
                                    Log.d(TAG,readMessage);

                                    for(int i=0; i==readMessage.length(); i++){
                                        if(readMessage.length() == 1)
                                            break;
                                        if(readMessage.charAt(i) != '#')
                                            if(flag1 == 0)
                                                latitude = latitude + readMessage.charAt(i);
                                            if(flag1 == 1)
                                                longitude = longitude + readMessage.charAt(i);
                                        if(readMessage.charAt(i) == '#') {
                                            flag1 = flag1 + 1;
                                            if(flag1 == 2)
                                                Log.d(TAG, "latitude=" + latitude);
                                        }
                                    }
}
                                }
                            });

                        }
                    }
                    catch (IOException ex)
                    {
                        stopThread = true;
                    }
                }
            }
        });

        thread.start();
    }



All I can see in the Logcat is the 6 lines of my gps data.
Any help with this?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1