Hi everyone,

I need some help, I am not sure whether I am using the Bluetooth API
in a wrong way or whether I discovered a bug. First of all, sorry for
all the verbose output, I already tried hard to shorten it and I hope
it is sufficient to understand the problem. Should you need more
output - I got plenty!

Thank you for your time!
Marek

Here is the short summary:

Serial Bluetooth connection blocks in readline of BufferedReader
although the GPSLogger sent more data, Connection library worked fine
on desktop linux machiene connected to GPSLogger via bluetooth.

Here is the long version:

I am attempting to write an application that connects my Motorola
Milestone (Droid) with Android 2.1 to my Holux M241 GPSLogger and to
download the contents of the memory. I want to be able to dump my
GPSLoggers memory to my mobile phone so that I can delete the
GPSLoggers memory contents and it can continue to log positions. I
read the source code to MTKBabel on linux for the protocol involved
and wrote some classes to communicate with the GPSLogger on my Android
phone. Before porting my communication library to Android I did try it
out with my desktop computer and everything worked fine (downloading
the whole memory consistently without problems). Then I ported the
communication library to Android and wrote several simple Activities
that enable the user to select the Bluetooth device and to connect and
download.


Problem description:

Unfortunately the download stalls unreproducibly after a random amount
of downloaded data. Sometimes (but very rarely) the download finishes
without problems but mostly my code stalls in the readline of the
BufferedReader that controls the InputStream of the serial bluetooth
connection.

I have attached the logcat output of my application and the hcidump
but do not understand the hcidump output (The F's are the GPSLogger's
memory contents which I deleted so you cannot see where I have
been ;-) - Download works from the desktop with other applications
(e.g. mtkbabel) ).

Here is my analysis so far:

In fact the last message received from the GPSLogger is the one at
2010-05-14 22:10:05.632079 in the hcidump output. Although more is
received in the hcidump output, it is not passed to the java
BufferedReader.



Logcat output:
V/HoluxM241Readline( 2687): Read line:$PMTK182,8,00034800, ### edited
to shorten debug (A LOT OF F's)
I/DownloadResponse( 2687): $PMTK182,8,00034800,#####edited to shorten
debug (A LOT OF F's)
V/HoluxM241Readline( 2687): before readline
V/HoluxM241Readline( 2687): Read line:$PMTK001,182,7,3*20
I/DownloadACK( 2687): $PMTK001,182,7,3*20
I/RequestingDownload( 2687): from 217088 to 219136 in 2097152
D/HoluxM241Packet( 2687): Sending packet :
$PMTK182,7,00035000,00000800*2C^M
V/HoluxM241Readline( 2687): before readline
V/HoluxM241Readline( 2687): Read line:$GPGGA,000141.200,8960.0000,N,
00000.0000,E,0,0,,137.0,M,13.0,M,,*45
V/HoluxM241Readline( 2687): before readline
V/HoluxM241Readline( 2687): Read line:$GPGSA,A,1,,,,,,,,,,,,,,,*1E
V/HoluxM241Readline( 2687): before readline
V/HoluxM241Readline( 2687): Read line:$GPGSV,1,1,00*79
V/HoluxM241Readline( 2687): before readline
V/HoluxM241Readline( 2687): Read line:$GPRMC,000141.200,V,8960.0000,N,
00000.0000,E,0.00,0.00,060180,,,N*78
V/HoluxM241Readline( 2687): before readline
V/HoluxM241Readline( 2687): Read line:$GPVTG,0.00,T,,M,0.00,N,
0.00,K,N*32
V/HoluxM241Readline( 2687): before readline

This is the last hcidump output:

2010-05-14 22:10:05.483245 < ACL data: handle 1 flags 0x02 dlen 41
    L2CAP(d): cid 0x0041 len 37 [psm 3]
      RFCOMM(d): UIH: cr 1 dlci 2 pf 0 ilen 33 fcs 0x9a
      $ P M T K 1 8 2 , 7 , 0 0 0 3 5 0 0 0 ,
      0 0 0 0 0 8 0 0 * 2 C . .
2010-05-14 22:10:05.487243 > HCI Event: Number of Completed Packets
(0x13) plen 5
    handle 1 packets 1
2010-05-14 22:10:05.632079 > ACL data: handle 1 flags 0x02 dlen 233
    L2CAP(d): cid 0x0041 len 229 [psm 3]
      RFCOMM(d): UIH: cr 0 dlci 2 pf 0 ilen 224 fcs 0x40
      $ G P G G A , 0 0 0 1 4 1 . 2 0 0 , 8 9
      6 0 . 0 0 0 0 , N , 0 0 0 0 0 . 0 0 0 0
      , E , 0 , 0 , , 1 3 7 . 0 , M , 1 3 . 0
      , M , , * 4 5 . . $ G P G S A , A , 1 ,
      , , , , , , , , , , , , , , * 1 E . . $
      G P G S V , 1 , 1 , 0 0 * 7 9 . . $ G P
      R M C , 0 0 0 1 4 1 . 2 0 0 , V , 8 9 6
      0 . 0 0 0 0 , N , 0 0 0 0 0 . 0 0 0 0 ,
      E , 0 . 0 0 , 0 . 0 0 , 0 6 0 1 8 0 , ,
      , N * 7 8 . . $ G P V T G , 0 . 0 0 , T
      , , M , 0 . 0 0 , N , 0 . 0 0 , K , N *
      3 2 . .
2010-05-14 22:10:05.632110 > ACL data: handle 1 flags 0x02 dlen 9
    L2CAP(d): cid 0x0041 len 5 [psm 3]
      RFCOMM(d): UIH: cr 0 dlci 2 pf 1 ilen 0 fcs 0x5c credits 1
2010-05-14 22:10:05.644470 > ACL data: handle 1 flags 0x02 dlen 338
    L2CAP(d): cid 0x0041 len 334 [psm 3]
      RFCOMM(d): UIH: cr 0 dlci 2 pf 0 ilen 329 fcs 0x40
      $ P M T K 1 8 2 , 8 , 0 0 0 3 5 0 0 0 ,
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F
2010-05-14 22:10:05.673003 > ACL data: handle 1 flags 0x02 dlen 212
    L2CAP(d): cid 0x0041 len 208 [psm 3]
      RFCOMM(d): UIH: cr 0 dlci 2 pf 0 ilen 203 fcs 0x40
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F
2010-05-14 22:10:05.713317 > ACL data: handle 1 flags 0x02 dlen 338
    L2CAP(d): cid 0x0041 len 334 [psm 3]
      RFCOMM(d): UIH: cr 0 dlci 2 pf 0 ilen 329 fcs 0x40
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F
2010-05-14 22:10:05.733917 > ACL data: handle 1 flags 0x02 dlen 192
    L2CAP(d): cid 0x0041 len 188 [psm 3]
      RFCOMM(d): UIH: cr 0 dlci 2 pf 0 ilen 183 fcs 0x40
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F
2010-05-14 22:10:05.781463 > ACL data: handle 1 flags 0x02 dlen 184
    L2CAP(d): cid 0x0041 len 180 [psm 3]
      RFCOMM(d): UIH: cr 0 dlci 2 pf 0 ilen 175 fcs 0x40
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F
2010-05-14 22:10:05.802093 > ACL data: handle 1 flags 0x02 dlen 338
    L2CAP(d): cid 0x0041 len 334 [psm 3]
      RFCOMM(d): UIH: cr 0 dlci 2 pf 0 ilen 329 fcs 0x40
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F F F F F F F F F F F F
      F F F F F F F F F
2010-05-14 22:10:05.802093 > ACL data: handle 1 flags 0x02 dlen 16
    L2CAP(d): cid 0x0041 len 12 [psm 3]
      RFCOMM(d): UIH: cr 0 dlci 2 pf 0 ilen 8 fcs 0x40
      F F F F F F F F


I boiled my code down into the following which is not very pretty but
also provokes the mentioned behaviour:


public class Downloader extends Activity {

        private String MAC;

        public static String LogMethodOnFull = "PMTK182,2,6";
        public static String Preamble = "$";
        public final static String PacketEnd="\r\n";

        public static String RequestDataDownload = "PMTK182,7,";
        public static String DataDownloadAnswer = "PMTK182,8,";
        public static String DataDownloadAck = "PMTK001,182,7,3";

        private static int flashMemorySize=16 * 1024 * 1024 / 8;

        private static int ChunkSize=0x800;

        protected void sendPkt(OutputStreamWriter writer, String payload){
                String towrite = Preamble+payload+"*"+generateChecksum(payload)
+PacketEnd;
                Log.d("HoluxM241Packet","Sending packet :"+towrite);
                try {
                        writer.write(towrite);
                        writer.flush();
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

        public static String generateChecksum(String string) {
                long checksum=0;
                String total=string;
                for(int i=0; i<total.length();i++)
                        checksum^= total.charAt(i);

                String crc = Long.toHexString(checksum).toUpperCase();
                if(crc.length()==1) return "0"+crc;
                else return crc;
        }

        private String waitPkt(String prefix,BufferedReader reader) throws
IOException {
                String line;
                while(true){
                        Log.v("HoluxM241Readline","before readline");

                                line= reader.readLine(); // <<< ==== blocks 
here !!


                        Log.v("HoluxM241Readline","Read line:"+line);
                        if(line.startsWith(Preamble+prefix)) break;
                }
                return line;
        }


        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent caller = getIntent();
        MAC = caller.getStringExtra("MAC");
        String name = caller.getStringExtra("NAME");

        Thread t = new Thread(new Runnable() {

                        @Override
                        public void run() {

                                UUID uuid =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
                                
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

                                BluetoothDevice remoteDevice =
BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MAC);

                                try {
                                        BluetoothSocket socket =
remoteDevice.createRfcommSocketToServiceRecord(uuid);
                                        socket.connect();

                                        Log.i("MarekService","connected To 
Holux");
                                        InputStream in = 
socket.getInputStream();
                                        OutputStream out= 
socket.getOutputStream();

                                        BufferedReader reader =new 
BufferedReader(new
InputStreamReader(in));
                                        OutputStreamWriter writer = new 
OutputStreamWriter(out);


                                        sendPkt(writer,LogMethodOnFull);

                                        int bytesToRead=flashMemorySize;
                                        int startOffset=0;

                                        for(int offset=startOffset; offset < 
bytesToRead; offset
+=ChunkSize){
                                                Formatter f = new Formatter();
                                                String payload = 
RequestDataDownload+f.format("%08X,%08X",
offset, ChunkSize);
                                                
Log.i("RequestingDownload","from "+ offset +" to "+(offset
+ChunkSize)+" in "+flashMemorySize);
                                                sendPkt(writer,payload);

                                                String datapkt = 
waitPkt(DataDownloadAnswer,reader);

                                                
Log.i("DownloadResponse",datapkt);
                                                datapkt = 
waitPkt(DataDownloadAck,reader);
                                                Log.i("DownloadACK",datapkt);

                                        }

                                } catch (IOException e) {
                                        Log.i("DownloadService","Exception in 
connection");

                                        e.printStackTrace();
                                }
                        }
                        });
                t.start();
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to