我在三星 Express (4.1.2) 上读取蓝牙套接字的写入时遇到了一个奇怪的问题。当我在我的 Google Nexus 4 (4.3) 上运行应用程序时,问题不会出现
当我尝试对套接字执行任何操作时,我的 Logcat 中会出现“同步锁定附加锁定打开”消息。通常紧随其后的是“同步锁定关闭”。这里的问题是,如果我尝试写入套接字,我得到一个 "genlock close" ,数据发送成功,但我无法访问输入流中的新数据,直到锁再次打开。
Logcat 中还会弹出许多“availableNative”消息。
这是我发送命令的代码,等待 0.5 秒并尝试读取输入缓冲区:
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;
}
}
如果我要在字节永远不会到达 inputStream 时运行此命令,但是如果我再次运行该命令,则所有字节从第一次开始就在那里,就像打开了闸门。我难住了。任何人都可以帮忙吗?(请注意,写入和读取命令都在应用程序中的其他位置起作用,并且蓝牙套接字对其他命令和响应起作用。
请询问您是否需要更多信息。