• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem parsing file

jonbonazza

Android Expert
Jul 13, 2010
1,930
457
Ok, so I am not sure if my problem is spawning from my parsing method or from the thread that contains it, but for some reason, once the file is downloaded, the parsing method doesn't even seem to be called.

My app receives a file via bluetooth and is *supposed* to parse it's contents and take the retrieved coordinates and plot them on a map overlay...

Here is the relevant code.

My thread that watches for incoming files:
Code:
private class AcceptThread extends Thread
    {
    	private final BluetoothServerSocket mmServerSocket;
    	
    	public AcceptThread()
    	{
    		BluetoothServerSocket tmp = null;
    		
    		try
    		{
    			tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("MYNAME", MY_UUID);
    		}
    		catch(IOException e)
    		{
    			
    		}
    		mmServerSocket = tmp;
    	}
    	
    	public void run()
    	{
    		BluetoothSocket socket = null;
    		
    		while(true)
    		{
    			try
    			{
    				socket = mmServerSocket.accept();
    			}
    			catch(IOException e)
    			{
    				break;
    			}
    			
    			if(socket != null)
    			{
    				Thread mConnectedThread = new Thread(new ConnectedThread(socket));
    				mConnectedThread.start();
    				try 
    				{
						mmServerSocket.close();
					} 
    				catch (IOException e) 
					{
					}
    			}
    		}
    	}
    }
    
    private class ConnectedThread extends Thread {
        private final InputStream mmInStream;

        public ConnectedThread(BluetoothSocket socket) 
        {
            InputStream tmpIn = null;
            

            // Get the BluetoothSocket input and output streams
            try 
            {
                tmpIn = socket.getInputStream();
            } 
            catch (IOException e) 
            {
            }

            mmInStream = tmpIn;
            
        }

        public void run() 
        {
            byte[] buffer = new byte[1024];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) 
            {
                try 
                {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer));
                            
    				List<String> list = readFile();
    				plotFix(list);
    				
    				mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DONE));
                    
                } 
                catch (IOException e) 
                {
                    break;
                }
            }
        }
    }


Here is my handler that is used int he above thread:

Code:
private final Handler mHandler = new Handler()
    {
    	@Override
    	public void handleMessage(Message msg)
    	{
    		switch(msg.what)
    		{
    		case MESSAGE_READ:
    			byte[] readBuf = (byte[])msg.obj;
    			FileOutputStream fos;
				try 
				{
					fos = new FileOutputStream("/sdcard/.EWA/MBBdata.txt");
					fos.write(readBuf);
					fos.close();
					
					
					//plotFix(list);
				} 
				catch (Exception e) 
				{
				
				}
				finally
				{
				info.setText("Read Successfully");
				}
    			break;
    		}
    	}
    };

and lastly, here is my parsing method:

Code:
public List<String> readFile()
	{
		File txtFile = new File("/sdcard/.EWA/MBBdata.txt");
		List<String> contents = new ArrayList<String>();
		BufferedReader input;
		
		
			try
			{
				input = new BufferedReader(new FileReader(txtFile));
				String line;
				info.setText("working...");
				while((line = input.readLine()) != null)
				{
					
					contents.add(line);
					info.setText("done...");
					
				}
				input.close();
			}
			catch(Exception e)
			{
			}
			
		return contents;
	}


Can anyone see what I am doing wrong? I have been trying to fix this problem for days now...
 
Is the file being saved properly? I'm not sure what the problem is, but if the save file is being executed asynchronously (not sure if it is), then the file could be written after it's trying to read it.

I've never used bluetooth, so there may be other issues -- have you tried putting a bunch of Log commands to check that the execution is what you expect?
 
Upvote 0
Is the file being saved properly? I'm not sure what the problem is, but if the save file is being executed asynchronously (not sure if it is), then the file could be written after it's trying to read it.

I've never used bluetooth, so there may be other issues -- have you tried putting a bunch of Log commands to check that the execution is what you expect?

but since the outputstream is being closed before the execution of the readFile() method, wouldn't that mean that the file save would have to have been completed?

As for the Log commands, That's not an option unfortunately. You see, I am unable to install the proper ADB drivers for some reason (probably has something to do with the fact that I have aftermarket usb drivers that are required for a tethering app I use) an the emulaters cannot emulate bluetooth... So I am unable to use the DDMS... :/
 
Upvote 0
Also, i should probably mention that after doin some more testing, i found that even if i remove all of the code in the handler and just leave, "info.setText(...);" The textviews text is still not set... i am wondering if the method is actually working and its a problem with the textview as the text view was my means of determining if it was working or not...
 
Upvote 0
Wait a second... Since I have a thread and a thread on top of that thread. If I use a handler in the second thread (the top one), will it post to the UI thread or the thread below it (the first thread)? If it's doing the latter, then that would be why my label isn't being altered.
 
Upvote 0
Ok I found the problem. The problem lies in the line

socket = mmServerSocket.accept();

If I place anything before that line it works, if I remove that line and put something in the run() method, it will work. Now even though my app runs without the accept() method being called, it still poses a problem. Now, it will execute w/e whatever is int he run() method as soon as it enters it. That's a problem, because I need it to wait until it retrieves a file before it does anything. I cannot find any methods or anything associated with BluetoothServerSocket that will allow me to check for that. the accept() method is suppsoed to block until a connection is established, but for some reason, even after establishing a connection, it would still block. Unless I am misunderstanding what the accept() method really does... At any rate, if I could just find a way to wait for the file to download to before it moves on, I would be set.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones