0

I am having a strange issue with reading an writing to/from the Bluetooth Socket on my Samsung Express (4.1.2). The issue does not present itself when I run the App on my Google Nexus 4 (4.3)

When I attempt to do any action on the socket i get a "genlock attach lock open" msg in my Logcat. Normally followed quickly with a "genlock close" . The issue here is that if I attempt to write onto the socket I get a "genlock close" , the data is sent successfully, but I cannot access new data in the input stream until the lock opens again.

Also there are numerous "availableNative" messeges popping up in the Logcat.

Here is the code where I send a command , wait for 0.5 second and attempt to read the input buffer:

public boolean ReadFaultTable()
{


    byte[] in_packet = new byte[100];

    int err_byte_count;
    int err_symp_count;


    if(!(BTCheck()))
            {
                Log.i(TAG,"ReadFaultTable() BT Fail!");
                //Toast.makeText(getApplicationContext(), "Socket not open for fault read", Toast.LENGTH_SHORT).show();
                return false;

            }


                byte [] out_packet = new byte[1];
                out_packet[0]= 0x0a;//READ_CURRENT_FAULT_CODES_CMD

                byte []full_packet=AddDRCheckSum(out_packet);

                connectedThread.clearBuffer();
                try{
                    Thread.sleep(200);
                   }
                catch(InterruptedException e){}

                connectedThread.write(full_packet);

                Log.i("Read Faults", "After Write");

            sleepForProcessingTime(500);
            Log.i("Read Faults", "0.5 Secs, Num Bytes = "+Integer.toString(connectedThread.bytesAvail()));


            if(connectedThread.bytesAvail() > 3)
            {
                //Log.i("Read Faults", "bytes avail = " + Integer.toString(connectedThread.bytesAvail()));
                for (int i =0;i<3;i++)
                {

                        Log.i("Read Faults", "In here");

                        if((in_packet[i]=connectedThread.readByte())==-1)
                        {
                            Log.i(TAG, "End of Input stream reached on iteration= "+ Integer.toString(i));
                            return false;
                        }
                        Log.i("Read Faults", "Byte read in =" +Integer.toHexString(in_packet[i]));
                }
            }
            else
            {
                Log.i("Read Faults", "No Bytes to read .. return false");
                return false;
            }

            /*
            if(connectedThread.bytesAvail() > 0)
            {
                in_packet= new byte[connectedThread.bytesAvail()];
                in_packet = connectedThread.read();
                Log.i("Read Faults","Bytes Read in =" + printCMD(in_packet));
            }
            */
            Log.i("Read Faults", "Made it here with "+ printCMD(in_packet));

            int fault_table_index =0;
              if (in_packet[0] == 0x0A && in_packet[1] == 0x0E )
              {
                  Log.i("Read Faults","in packet passed checks");
                  err_byte_count=in_packet[2];
                  fault_table= new boolean[err_byte_count*8];
                  Log.i("Read Faults","err byte count = "+ Integer.toString(err_byte_count));
                  for (int i=0;i<(err_byte_count);i++)
                  {
                      int val =connectedThread.readByte();
                      Log.i("Read Faults","Next byte read in =0x" + Integer.toHexString(val));
                      in_packet[3+i]=(byte)val;

                      int mask =0x01;
                      for (int x = 0; x < 8; x++)
                      {
                          if ((val & mask)== mask)
                          {
                              Log.i("Read Faults","Fault Index "+ Integer.toString(fault_table_index +1) + "true");
                              fault_table[fault_table_index++]=true;
                          }
                          else
                          {
                              Log.i("Read Faults","Fault Index "+ Integer.toString(fault_table_index +1) + "false");
                              fault_table[fault_table_index++] = false;
                          }
                          mask <<= 1;
                      }


                  }
                  Log.i("Read Faults","Made it to here");
                  in_packet[3 + err_byte_count] = (byte)connectedThread.readByte();

                  err_symp_count = in_packet[3 + err_byte_count];

                  symp_table = new byte[err_symp_count];
                  for (int i=0; i < err_symp_count; i++)
                  {
                      in_packet[3 + err_byte_count + 1 + i] = (byte)connectedThread.readByte();
                      symp_table[i] = in_packet[3 + err_byte_count + 1 + i];
                  }
                   return true;
              }
              else
              {
                  return false;
              }

    }

If I was to run this command once the bytes will never reach the inputStream , but if i ran the command again, all of the bytes are there from the first time, it's like a flood gate gets opened. I'm stumped. Can anyone help ? (Note the write and read commands all work else where in the app and the Bluetooth socket is functioning for other commands and responses .

Please ask if you require further info.

Jay Long
  • 3
  • 2

1 Answers1

0

Just found a work around. For whatever reason the phone did not like the idea of reading more than one byte off the Bluetooth Socket at a time. So I implemented a byte by byte approach that has provided an acceptable patch. I didn't pick up on the socket reporting one byte available before because I was checking for at least 3 bytes on the socket before reading in (this makes sense for my purposes). Phone continually reports 1 byte available on the inputStream, so you take a byte while the inputStream reports one. Would still be interested in an explanation if someone has one. Thanks

Jay Long
  • 3
  • 2