Problem parsing file


Last Updated:

  1. jonbonazza

    jonbonazza Well-Known Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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 (Text):
    1. private class AcceptThread extends Thread
    2.     {
    3.         private final BluetoothServerSocket mmServerSocket;
    4.        
    5.         public AcceptThread()
    6.         {
    7.             BluetoothServerSocket tmp = null;
    8.            
    9.             try
    10.             {
    11.                 tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("MYNAME", MY_UUID);
    12.             }
    13.             catch(IOException e)
    14.             {
    15.                
    16.             }
    17.             mmServerSocket = tmp;
    18.         }
    19.        
    20.         public void run()
    21.         {
    22.             BluetoothSocket socket = null;
    23.            
    24.             while(true)
    25.             {
    26.                 try
    27.                 {
    28.                     socket = mmServerSocket.accept();
    29.                 }
    30.                 catch(IOException e)
    31.                 {
    32.                     break;
    33.                 }
    34.                
    35.                 if(socket != null)
    36.                 {
    37.                     Thread mConnectedThread = new Thread(new ConnectedThread(socket));
    38.                     mConnectedThread.start();
    39.                     try
    40.                     {
    41.                         mmServerSocket.close();
    42.                     }
    43.                     catch (IOException e)
    44.                     {
    45.                     }
    46.                 }
    47.             }
    48.         }
    49.     }
    50.    
    51.     private class ConnectedThread extends Thread {
    52.         private final InputStream mmInStream;
    53.  
    54.         public ConnectedThread(BluetoothSocket socket)
    55.         {
    56.             InputStream tmpIn = null;
    57.            
    58.  
    59.             // Get the BluetoothSocket input and output streams
    60.             try
    61.             {
    62.                 tmpIn = socket.getInputStream();
    63.             }
    64.             catch (IOException e)
    65.             {
    66.             }
    67.  
    68.             mmInStream = tmpIn;
    69.            
    70.         }
    71.  
    72.         public void run()
    73.         {
    74.             byte[] buffer = new byte[1024];
    75.             int bytes;
    76.  
    77.             // Keep listening to the InputStream while connected
    78.             while (true)
    79.             {
    80.                 try
    81.                 {
    82.                     // Read from the InputStream
    83.                     bytes = mmInStream.read(buffer);
    84.  
    85.                     // Send the obtained bytes to the UI Activity
    86.                     mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer));
    87.                            
    88.                     List<String> list = readFile();
    89.                     plotFix(list);
    90.                    
    91.                     mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DONE));
    92.                    
    93.                 }
    94.                 catch (IOException e)
    95.                 {
    96.                     break;
    97.                 }
    98.             }
    99.         }
    100.     }

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

    Code (Text):
    1. private final Handler mHandler = new Handler()
    2.     {
    3.         @Override
    4.         public void handleMessage(Message msg)
    5.         {
    6.             switch(msg.what)
    7.             {
    8.             case MESSAGE_READ:
    9.                 byte[] readBuf = (byte[])msg.obj;
    10.                 FileOutputStream fos;
    11.                 try
    12.                 {
    13.                     fos = new FileOutputStream("/sdcard/.EWA/MBBdata.txt");
    14.                     fos.write(readBuf);
    15.                     fos.close();
    16.                    
    17.                    
    18.                     //plotFix(list);
    19.                 }
    20.                 catch (Exception e)
    21.                 {
    22.                
    23.                 }
    24.                 finally
    25.                 {
    26.                 info.setText("Read Successfully");
    27.                 }
    28.                 break;
    29.             }
    30.         }
    31.     };
    and lastly, here is my parsing method:

    Code (Text):
    1. public List<String> readFile()
    2.     {
    3.         File txtFile = new File("/sdcard/.EWA/MBBdata.txt");
    4.         List<String> contents = new ArrayList<String>();
    5.         BufferedReader input;
    6.        
    7.        
    8.             try
    9.             {
    10.                 input = new BufferedReader(new FileReader(txtFile));
    11.                 String line;
    12.                 info.setText("working...");
    13.                 while((line = input.readLine()) != null)
    14.                 {
    15.                    
    16.                     contents.add(line);
    17.                     info.setText("done...");
    18.                    
    19.                 }
    20.                 input.close();
    21.             }
    22.             catch(Exception e)
    23.             {
    24.             }
    25.            
    26.         return contents;
    27.     }
    28.    

    Can anyone see what I am doing wrong? I have been trying to fix this problem for days now...
     

    Advertisement
  2. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    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?
     
  3. jonbonazza

    jonbonazza Well-Known Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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... :/
     
  4. jonbonazza

    jonbonazza Well-Known Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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...
     
  5. jonbonazza

    jonbonazza Well-Known Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.
     
  6. jonbonazza

    jonbonazza Well-Known Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Well, I tried placing a second handler (containing the info.setText(...); command) in the first handler's handleMessage(); method. Still didn't work... This is really, really weird...
     
  7. jonbonazza

    jonbonazza Well-Known Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.
     

Share This Page

Loading...