Strange bluetooth locking behavior on Samsung Express - android

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.

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

Related

Android TCP connection receiving packets out of order

I have a Particle Photon microcontroller sending TCP packets over a hotspot WiFi network to an Android Phone. The microcontroller is acting as server, the phone as client.
The phone however is receiving some (but not all) of the packets out of order, despite the information being transmitted via tcp. It was my understanding that this would not happen - am i wrong, or is there something i can do to correct this?
Microcontroller Code:
// This #include statement was automatically added by the Particle IDE.
#include "databuffer5.h"
// This #include statement was automatically added by the Particle IDE.
#include "databuffer7.h"
const unsigned int localPort = 10000;
IPAddress remoteIP(a, b, c, d);
// An TCP instance to let us send and receive packets over wifi
TCPServer server = TCPServer(localPort);
TCPClient client;
// UDP Port used for two way communication
short msg_count = 0;
const int adcPin = A0;
int byteBuffer;
unsigned long loopTimer;
const int packetSize = 40; //number of bytes in packet - 10 ints with 4 bytes each
byte buffer[packetSize];
int j = 0;
int dataCount = 0; //dummy data that increments every loop point, to measure packet contiuity
//(creates a line with slope 1 as data)
void setup() {
// start the UDP
server.begin();
// Print your device IP Address via serial
Serial.begin(9600);
Serial.println(WiFi.localIP());
//Serial.println(System.ticksPerMicrosecond()); //returns 120, ie 120MHz
}
void loop()
{
if (client.connected())
{
loopTimer = millis(); //mark start time of loop
for (int i = 0; i < 10; i++)
{
//for testing connection
byteBuffer = i+j*10;
buffer[i*4] = ( (byteBuffer >> 24) & 0xFF); //take upper 8 bits
buffer[i*4+1] = ( (byteBuffer >> 16) & 0xFF); //take middle upper 8 bits
buffer[i*4+2] = ( (byteBuffer >> 8 ) & 0xFF); //take middle lower 8 bits
buffer[i*4+3] = ( byteBuffer & 0xFF); //take lower 8 bits
dataCount++;
if (i != 9)
{
while(millis() < (loopTimer+10*(i+1))); //ie do nothing for 10 ms (time is in ms, want to delay by exactly 10ms for each loop)
//goal here is to sample every 10ms, by delaying for the remaining time
//dont delay here for the last sample, as the udp packet will take time
//delay after instead
}
}
server.write(buffer, sizeof(buffer)); //using sizeof on a byte array so dont need to scale (ie scaling factor is 1)
j++;
if (j < 0){j = 0;}
while(millis() < (loopTimer+10*10)); //delay till 100ms after loops started
}
else
{
client = server.available();
}
}
Android (client) code:
async_udp = new AsyncTask<Void, int[], Void>() {
#Override
protected Void doInBackground(Void... params) {
byte b1[];
b1 = new byte[100];
while (serverActive) {
Socket socket = null; //previously this was DatagramSocket (UDP) - no Socket (TCP)
try {
//DatagramSocket s = new DatagramSocket(server_port, server_ip);
socket = new Socket(server_ip, server_port);
socket.setPerformancePreferences(1, 2, 2);
InputStream socketStream = socket.getInputStream();
DatagramPacket p1 = new DatagramPacket(b1, b1.length);
ByteBuffer wrapped;
int data[] = new int[10+1]; //first number for message data, second is for the message number
while (serverActive) //TODO include shutdown function
{
while (socketStream.available() < 39){}
socketStream.read(b1, 0, 40);
//packet structure is a char containing message number, and 10 shorts (2 bytes) containing data points (between 0 and 4096)
wrapped = ByteBuffer.wrap(Arrays.copyOfRange(b1, 0, 40)); // extract 40 bytes to convert to 10 ints
for (int i = 0; i < 10; i++) {
data[i] = wrapped.getInt();
}
String str = data.toString();
server_port = p1.getPort();
server_ip = p1.getAddress();
String str_msg = "RECEIVED FROM CLIENT IP =" + server_ip + " port=" + server_port + " message no = " + b1[0] +
" data=" + str; //first character is message number
statusText = str_msg;
publishProgress(data);
}
socketStream.close();
socket.close();
} catch (SocketException e) {
if (socket != null) {}
//status.append("Error creating socket");
statusText = (" Error creating socket"); //this doesn't work!
} catch (IOException e) {
//status.append("Error recieving packet");
statusText = (" Error receiving packet"); //this doesn't work!
}
try{
Thread.sleep(100, 0); //sleep for 10ms if no wifi lock is found, to stop battery being silly
} catch(Exception e){
e.printStackTrace();
}
}
return null;
}
protected void onProgressUpdate(int[]... data1)
{
super.onProgressUpdate(data1);
int data[] = data1[0];
//send data to graph
for (int i = 0; i < 9; i++)
{
series.appendData(new DataPoint(lastDataX++, data[i]), false, graphPointsMax);
//append 9 points to graph, but only redraw the grpah on the 10th
}
series.appendData(new DataPoint(lastDataX++, data[9]), true, graphPointsMax);
}
};
if (Build.VERSION.SDK_INT >= 11)
{
async_udp.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else
{
async_udp.execute();
}
What is guaranteed by TCP, is that when you send a message - it will come in the right order, even if split by the TCP stack along the way (see TCP Segment Number, which serves as an indication which split message part belongs together with which, and in what order). So, in your case, all you are sending using:
server.write(buffer, sizeof(buffer));
no meter how big is the buffer (within the limits of TCP protocol, of course) is guaranteed to arrive in the right order and complete.
Sending several buffers one-by-one over TCP will provide you guaranteed delivery, or error notification (if any), but not the order of the messages sent. Down to the protocol level, these packets will contain different Sequence Numbers, and therefore will be treated by the TCP stack of the receiver separately, in the order the stack prefers.
I see two relatively simple things that could be done here:
try to put all you can in one buffer (not really practical approach, though)
Introduce the counter and increment each time you are sending the buffer out. Put the counter along with the buffer and check the precedence on the receiving side (phone, in your case).

Sending continuous data from arduino to Android App using HC-05

I've been struggling with the problem of sending continuous data from arduino to Android.
What I want to do is get analog read convert it to 0-5V information, and send that information to Android app.
My arduino code is just simply:
//(...)defining pins and levels
SoftwareSerial BTSerial(rxPin, txPin);
void setup()
{
pinMode(getData, INPUT);
digitalWrite(keyPin, LOW);
BTSerial.begin(9600);
}
void loop()
{
contact = digitalRead(getData);
if (contact == HIGH) {
sensorValue = analogRead(sensorPin);
double voltage = sensorValue * (5.0 / 1023.0);
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
BTSerial.println(voltage, 3);
BTSerial.write("\r");
if (Serial.available()) {
BTSerial.write(Serial.read());
}
}
delay(5);
}
I need to send data informing about measurment with ~200Hz frequency.
After sending the data to application it seems that part of data is lost.
I tried higher bound rates but the problem still occurs. Is there a way to send continuous data from arduino using serial port without loosing some % of that data?
I think the problem is in the design of the receiver. I Solved BTL communication in .net Xamarin, but the principle should be the same. In Android reading from InputStream must be quick and can not use sleep. You need to use an endless cycle and there quick read data into temp buffer. Immediately a dune bytes to an auxiliary large buffer (use read / write cursor) and then, for example, in timer treat the data (I suppose you are using some packet protocol)
public override void Run()
{
WriteLogInfoToLog("ConnectedThread.Run() - before");
while (true)
{
try
{
int readBytes = 0;
lock (InternaldataReadLock)
{
readBytes = clientSocketInStream.Read(InternaldataRead, 0, InternaldataRead.Length);
Array.Copy(InternaldataRead, TempdataRead, readBytes);
}
if (readBytes > 0)
{
lock (dataReadLock)
{
dataRead = new byte[readBytes];
for (int i = 0; i < readBytes; i++)
{
dataRead[i] = TempdataRead[i];
}
}
}
}
catch (System.Exception e)
{
btlManager.btlState = BTLService.BTLState.Nothing;//Spadlo spojeni, musi spustit cele od zacatku
WriteLogInfoToLog("ConnectedThread.Run() - EXCEPTION " + e.Message + ", " + e.HResult + ", " + e.StackTrace + ", " + e.InnerException);
if (e is Java.IO.IOException)
{
}
else
{
}
break;
}
}
WriteLogInfoToLog("ConnectedThread.Run() - after");
}

Android Bluetooth InputStream real time read

I am working on an Android application that receives a real time data by Bluetooth and plots it on the screen.
The data is a gyro sensor position information. I am sending it from a custom Freescale Kinetis K10 microcontroller board (designed and tested by myself). For the Bluetooth communication I am using HC-05 Bluetooth module.
The format of the data is as follows:
byte_1: position identification byte, always equals to -128
byte_2: position of axis 1
byte_3: position of axis 2
byte_4: position of axis 3
I am sending these 4 bytes continuously one after another, in that particular order. I am sending this packet of 4 bytes every 5 ms and sending the packet takes about 4.7 ms (9600 baud rate).
The data output from the microcontroller is perfect in terms of accuracy and timing (checked with a logic analyzer).
The problem is that when it is being received from the phone, some of the bytes seem to get lost. Here is the part of the code, where I am reading the InputStream:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e("Printer Service", "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
#Override
public void run() {
Log.i("BluetoothService", "BEGIN mConnectedThread");
byte[] buffer = new byte[4];
int bytes;
while (true) {
try {
bytes = mmInStream.read(buffer);
int position = 0;
if(buffer[0] == -128) {
if(bytes >= 2) {
sendArray.errorTilt = buffer[1];
}
if(bytes >= 3) {
sendArray.errorRoll = buffer[2];
}
if(bytes == 4) {
sendArray.errorPan = buffer[3];
}
}
else if(buffer[1] == -128) {
position = 1;
if(bytes >= 3) {
sendArray.errorTilt = buffer[2];
}
if(bytes == 4) {
sendArray.errorRoll = buffer[3];
}
if(bytes >= 2) {
sendArray.errorPan = buffer[0];
}
}
else if(buffer[2] == -128 && bytes >= 3) {
position = 2;
sendArray.errorRoll = buffer[0];
sendArray.errorPan = buffer[1];
if(bytes == 4) {
sendArray.errorTilt = buffer[3];
}
}
else if(buffer[3] == -128 && bytes == 4) {
position = 3;
sendArray.errorTilt = buffer[0];
sendArray.errorRoll = buffer[1];
sendArray.errorPan = buffer[2];
}
if(position <= bytes && bytes > 1) {
sendArray.errorUpdate = true;
}
} catch (Exception e) {
e.printStackTrace();
connectionLost();
BluetoothService.this.stop();
break;
}
}
}
public void write(int oneByte) {
try {
mmOutStream.write(oneByte);
} catch (IOException e) {
Log.e("BluetoothService", "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e("BluetoothService", "close() of connect socket failed", e);
}
}
}
sendArray is a singleton that keeps many different variables.
errorTilt, errorRoll and errorPan are the current values of the axis, which are being updated from the receiving buffer.
"position" keeps the position of the position identification byte. It is used for a check if any variables have been updated.
Many times just one byte is received in the input buffer and since I don't know which axis is that supposed to be, since I don't have information about it's relative position to the position byte, this particular byte is useless and gets lost.
I've tested the accuracy of receiving by the following method. I made the MCU output a triangular wave on one of the axis, instead of the axis data. On the phone the lines of the triangular wave are not straight as they are supposed to be, but randomly curved and containing artifacts.
To plot the data I am using GraphView and I am updating the graph on equal intervals from a separate thread.
I've tried using longer receiving buffer (with a modified receiving algorithm), but this doesn't help as only a few bytes are being received at a time.
I've tried implementing InputStream.available() but it was always giving 127 bytes available, which didn't seem to be true.
I've read many threads about similar problems and I spent the last 5 days working on it, but I couldn't find a good solution.
To summarize, I need to achieve accurate, real time (or close to real time) receiving of all the bytes.
Thread with a similar problem:
How to do good real-time data streaming using Java Android SDK
Thank you.
UPDATE:
I've tried sending the information just for one of the axis, so it is simple and clear, without the need of position bytes. I was sending it again every 5 ms, but this time it was more time between the consecutive bytes, since it's just one byte in the packet.
I used InputStream.read() this time, which doesn't require a buffer. However, the incoming data was corrupted again, because random bytes couldn't be received.
I've seen different project using this method successfully, I don't know why it's not working with me. I thought it might be a problem with the HC-05 Bluetooth module I'm using, but I tried a different one - HC-06, and the situation is the same. I haven't tried a different phone, but my phone (Samsung Galaxy S3, Android 4.1.2) seems to be working OK.
UPDATE2: I've tried again testing the original code with InputStream.available() before reading from the stream.
When the condition is available()>0, there are no major changes, maybe it works slightly worse.
When the condition is available()>1, it never reads. I guess that is because of the unreliable available() method, as it says in the documentation.
you have incorrect processing of data, if you want to get data from microcontroller board. You have to use bytesAvaliable because android bluetooth Socket is pretty slow over microcontroller boards with bluetooth. But "bytesAvaliable way" has nuance - As socket is slow receiver, bytesAvaliable can catch more then 1 packet from board in one time so you gotta devide readed data by yourself, Check my code out below! My code is getting 38 bytes packets from inertial sensor so you gotta only change count of bytes! 0xAA is the first byte of every next packet so if you find 0xAA byte and have 38 bytes you get packet and nullify iterator. But anyway I'm sure that you still can sometimes lose data because it's not high frequency data transfering way
public void run() {
byte[] bytes = new byte[38];
int iterator = 0;
while (true) {
try {
int bytesAvailable = mmInStream.available();
if (bytesAvailable > 0) {
byte[] curBuf = new byte[bytesAvailable];
mmInStream.read(curBuf);
for (byte b : curBuf) {
if (b == (byte) 0xAA && iterator == 38) {
mHandler.obtainMessage(MainActivity.DATA_READ, bytes.length, -1, bytes).sendToTarget();
iterator = 0;
bytes[iterator] = b;
} else {
bytes[iterator] = b;
}
iterator++;
}
}
} catch (IOException ex) {
Log.e(TAG, "disconnected", ex);
connectionLost();
break;
}
}
}

android bluetoothsocket receive zero-value bytes?

Got a problem at receiving bytes from bluetooth socket. I'm trying to send message created by arduino - couple of single bytes values i.e. 0x41, 0x05, 0xFF(...) into my phone with android. It works fine until one of these value is zero (0x00). Transmission hangs until new message comes. Anyone meet with that situation?
My "reader" works in separate thread, processBuffer() do sth with data that should be received:
public void run() {
while(stop == false){
try {
bytes = InStream.read(readbuffer);
for (int i = 0; i < bytes; i++){
Log.d("FRAME", "Read bytes "+readbuffer[i]);
}
Log.d("FRAME", "Read number of bytes "+bytes);
processBuffer(bytes);
} catch (Exception e){
Log.d("BT_debug", "Cannot read bytes");
Log.d("BT_debug", "iterator: "+iterator);
e.printStackTrace();
break;
}
}
}

Android communication, what is sent is not what is received

Moved from Android forums
Im at a loss here. It want to send some serial data from arduino to android but what I send is not what is received. For example, If I put Serial.write(5), on the android side I get 48. If I put Serial.write(6) I get 1. The same problem happens when I send characters. If I send a 't', on android I get a T with 2 points on top(as if its a character from another language). ??????? What's happening?
Arduino
int count = 5;
const unsigned int BAUD_RATE = 115200;
void setup() {
Serial.begin(BAUD_RATE);
delay(10000);
}
void loop() {
//Serial.print(count, DEC);
//Serial.print(count, HEX);
Serial.print(count, OCT);
//Serial.println(count, BIN);
//Serial.write(temp);
//Serial.write("t");
Serial.write(count);
count++;
delay(2000);
}
Android
RFComm: UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
byte[] buffer = new byte[255];
Handler myHandler1 = new Handler();
Runnable receiveValues = new Runnable() {
public void run() {
try {
while (connected) {
while (in.available() > 0) {
Log.v("Note",
"For value: " + String.valueOf(in.read(buffer)));
for (int x = 0; x < buffer.length; x++)
Log.v("Note", "A " + buffer[x]);
}
Thread.sleep(50);
}
} catch (Exception e) {
connected = false;
e.printStackTrace();
}
}
};
The timing is right, every two seconds something gets sent but still I get the wrong value. As you can also see, I tried other ways to write to the serial port on the arduino but none of them work. Another thing, the end of line(when I do Serial.println()) is consistently received as a 0 on android.
Running on android 2.3.7 where min sdk = 8 (android 2.2)
Answer is really simple... Forgot about int ranges(-32768 to +32767) or in this case unsigned int(0 - 65534) and when I directly entered the baud rate, it worked well.
Serial.begin(115200);

Categories

Resources