Based on SDK's bluetoothchat example I'm working on an app that transmits strings between an android device and arduino.
I've the folowing issues:
1- If I use this code I loose the first byte sent by arduino:
// Keep listening to the InputStream while connected
while (true) {
try {
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes,-1, buffer).sendToTarget();
But This way it works :
bytes = mmInStream.available();
if(bytes != 0) {
SystemClock.sleep(100); //pause and wait for rest of data.
bytes = mmInStream.available(); // how many bytes are ready to be read?
bytes = mmInStream.read(buffer, 0, bytes); // record how many bytes we actually read
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
}
Any explanation please?
2- Arduino sends the string "OK" when recieves a string from the device.
How to use this as an ACK(nowledgment) in my app ?
I tried this but with no success:
String ack = ""; //global variable
sendstring("test string");// send a test string to arduino
SystemClock.sleep(100); //wait for arduino response
if(ack.equals("OK")) txtv.setText(" well received"); //well done
in the handler:
if(msg.what == Bluetooth.MESSAGE_READ){
String receivedstring = new String((byte[]) msg.obj, 0, msg.arg1);
ack = receivedstring ;
I don't get ack = "OK" , and " well received" is not displayed in the text view !!
Many thanks for ur help
Hi i dont know that much about blutoothchat but i may have an anwser to your first question. If you dont already have an answer.
// Keep listening to the InputStream while connected
while (true) {
try {
bytes = mmInStream.read(buffer); // it may not work because its not reading from the first line unlike this: bytes = mmInStream.read(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes,-1, buffer).sendToTarget();
Related
I am sending accelerometer data from STM32 microcontroller to a HC-06 Bluetooth module, and I have developed an Android app to receive the data.
On STM32, I make a 3 byte packet (uartBuffer) which has the following structure: [0] counter, [1] xdata, [2] ydata.
I then send this data via UART to the HC-06 module.
HAL_UART_Transmit(&uartHand, uartBuffer, 3, 10); // {uartHandle, data, dataSize, timeout}
On Android, I have no problem connecting with the device, etc. However, when I receive the packet, it doesn't arrive together. It does always arrive eventually (in the correct order as well), but I might get a packet with only the 1st byte, then a packet with the last 2 bytes, and sometimes other combinations, as can be seen in the logcat picture below. The value which always increments by one (i.e. 20, 21, 22, etc..) is the 1st byte of the packet as sent by STM32.
I have tested this with different sampling rates between up to 100Hz, and with bigger packets (up to 5 bytes).. I never lose any bytes, but the packet arrangement is further divided at higher sampling rates and data sizes.
Here is the ConnectedThread class which does the reading of data in the run method.
`
private class ConnectedThread extends Thread {
private final BluetoothSocket mmBTSocket;
private final InputStream mInStream;
private final OutputStream mOutStream;
public ConnectedThread(BluetoothSocket mSocket) {
Log.d(TAG, "ConnectedThread: Starting");
mmBTSocket = mSocket;
InputStream mTempIn = null;
OutputStream mTempOut = null;
try {
mTempIn = mmBTSocket.getInputStream();
mTempOut = mmBTSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "Failed to get I/O Stream", e);
}
mInStream = mTempIn;
mOutStream = mTempOut;
}
public void run(){
byte[] mBuffer = new byte[3]
int mBytes = 0; // bytes returned from read
String mIncomingMessage = new String();
mIncomingStringBuilder = new StringBuilder();
while (true) {
try {
mIncomingStringBuilder.setLength(0);
mBytes = mInStream.read(mBuffer, 0, 3);
for (int i=0; i<mBytes; i++) {
mIncomingMessage = String.valueOf(mBuffer[i]);
mIncomingStringBuilder.append(mIncomingMessage + ",");
}
Log.d(TAG, "run: ConnectedThread - InputStream: " + mIncomingStringBuilder);
Intent incomingMessageIntent = new Intent("incomingMessage");
incomingMessageIntent.putExtra("theMessage", mIncomingStringBuilder.toString());
LocalBroadcastManager.getInstance(mContext).sendBroadcast(incomingMessageIntent);
} catch (IOException e) {
Log.e(TAG, "error reading from inputStream, e);
break;
}
}
}
`
So my questions are:
1) Why am I not receiving the full packets in the same structure as they are sent from the STM32? A commentor here mentions here that:
The bluetooth connection is stream based, not packet based. There is no guarantee or attempt to preserve packetization. So any number of writes can result in any number of reads, just the stream of bytes are guaranteed to be correct. If you need to detect packets, you need to provide your own packet structure to wrap your data.
Is this true? Can Bluetooth really not preserve the full packet structure? Can anyone verify this?
2) If this is true, then what is the best method to construct a package which is easy for Android to detect the start of and end? I assume I need a sync/start byte. How do I ensure this sync byte doesn't occur in my accelerometer data? Should I use 2 consecutive sync bytes? Is a checksum at the end of the packet enough to detect if the packet has errors?
3) Finally, is it good practice to only use connectedThread on Android to receive the byte, convert to string and send to main activity so that its free to receive the next as quickly as possible? Then in main activity, I would reconstruct the packet? Or would this be too much work for main activity too?
Thanks in advance for your help.
I am trying to send audio file via Bluetooth using the sample BluetoothChat code.
I have created a class that converts audio into bytes and vise-versa.
Here are the modifications I have made in the sample code
private void sendMessage(String message) {
// Check that we're actually connected before trying anything
if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
Toast.makeText(getActivity(), R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
// Check that there's actually something to send
if (message.length() > 0) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = message.getBytes();
//Convert noti.mp3 into byte array (noti.mp3 is an audio file I am using for testing)
try{send = Converter.convertAudioToBytes(path+"noti.mp3");}
catch(Exception e){Logger.append("Error while converting audio into bytes", e);}
mChatService.write(send);
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
mOutEditText.setText(mOutStringBuffer);
}
}
I am also adding some bytes at the end of the audio to identify the endOfFile on the receiving end.
On the receiving end I have updated the mHandler
//List to store bytes received from the other device
private List<Byte> list = new ArrayList<Byte>();
case Constants.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
//Status for end of data
boolean endOfData = false;
//Add bytes to the Array list
for(int i=0;i<msg.arg1;i++){list.add(readBuf[i]);}
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage);
//Check if the received message contains this string
//Also check if the length of the data is less than 990
if(readMessage.contains("END OF AUDIO FILE") && msg.arg1<990){
endOfData = true;
}
//Check if all the data has been received
if(endOfData)
{
try{
Converter.convertBytesToAudio(list,path);
mConversationArrayAdapter.add(mConnectedDeviceName + ": Audio saved!");
}
catch(Exception e){
Logger.append("Error while converting bytes to audio", e);
}
finally{
list.clear();
}
}
break;
The code works fine for small audio files but when I send a file that is bigger than 3KB it does not properly receive the data and does not transfer it to audio properly.
I tried to send some long text like this
String str = "Message number ";
for(int i=0;i<1000;i++){message += str+i+" ,\n";}
byte[] send = message.getBytes();
mChatService.write(send);
on the receiving end I noticed, the messages received are not synchronized.
Can someone please help me solve this issue, or any other way to transfer audio messages via Bluetooth. I am trying to do something like WhatsApp audio messages but via Bluetooth.
Any help is appreciated.Thanks
Still waiting for an answer :( Please help
I am sending data from an arduino through a Bluetooth module. I am simply streaming different integers;
int bluetoothData;
//give value to bluetoothData
Serial1.println(bluetoothData);
In my Android code, I have a Bluetooth socket that receives the data from the arduino. The data is taken care of by a Handler. And here lies the problem, sometimes the data is misread. For instance, I am sending '100', but instead the Handler saves in my database '000' or I send '194' and what is read is '994'. Sometimes such a reading mistake would occur once in every 15. Sometimes there will not be any mistake at all. Does someone know what may the reason be? Is it some hardware malfunction of the bluetooth module, is it the way I am reading the data, etc. Here is the Android code for the Thread receiving data:
final int RECIEVE_MESSAGE = 1;
...
mmInStream = socket.getOutputStream();
byte[] buffer = new byte[256]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
bytes = mmInStream.read(buffer);
h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();
}
Here is the code for the handler itself:
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RECIEVE_MESSAGE:
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, msg.arg1);
sb.append(strIncom);
int endOfLineIndex = sb.indexOf("\r\n");
if (endOfLineIndex > 0) {
String sbprint = sb.substring(0, endOfLineIndex);
sb.delete(0, sb.length());
btData = Integer.parseInt(sbprint);
M = btData;
insert = mDataBaseHelper.insert(M,DataBaseHelper.TABLE_Temp);
}
I use some of the Bluetooth chat samplecode for sending a SMALL (177 byte to 3617 byte) "settings-file" "securly" between apps.
when it is under 1024 bit everything works fine: (so the 177 works PERFECT)
sendingdevice press "send button" and the reciver gets it (with a dialog if they want it..) (and I save the "string" to a "settings"file on that device)
but if the file is over 1024 it gets chunkt/cut off.. (example: 2000byte)
so the file gets corrupted (data-loss but some info remains..)
Probably I need to "split" my file in 1024 bits and send the bits and in the receiver-end, I need to "add them all up"..
but I don't know the "standard best practices" for this, do you have any suggestions?
I have tryed to "only higher" the 1024 byte to 65536byte, but that don't work..
(or maby I do this wrong..)
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothChatService.this.start();
break;
}
}
}
sedan write:
/**
* Write to the connected OutStream.
* #param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
mmOutStream.flush();
// Share the sent message back to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
and when i "click on send settings":
String message = view.getText().toString();
String settingInAString = getSettingInALargeString();
sendMessage(settingInAString);
and in "sendMessage":
if (message.length() > 0) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = message.getBytes();
mChatService.write(send); //SO convert to byte and then send the byte..
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
mOutEditText.setText(mOutStringBuffer);
}
and:
/**
* Write to the ConnectedThread in an unsynchronized manner
* #param out The bytes to write
* #see ConnectedThread#write(byte[])
*/
public void write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
if (mState != STATE_CONNECTED) return;
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
}
but I think you know what Im lookning for ...
(or can I some how change the "BluetoothChat" so it can sent and recive a large Sring, and not "byte:s"? :-) )
Best REGARDS to you all :-)
EDIT:
on the reader side I have this:
on the "reader end" I have:
....
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
//only a byte redebuffer, hmm can I change this? or do i use a whileloop?
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
recivedStringCheckFirst(readMessage);
//simple-check if the data is a "data-setting-file"
String [] allSettingsInALargeArray1 = doSplitOnLargeString(readMessage);
int titleArrayLength1 = getLengthOffTheUpCommingTitleArrayFromNew(allSettingsInALargeArray1); //this do a split and looks if it is 1,2,3..20 settings..)
mConversationArrayAdapter.add(titleArrayLength1 + " datasettings recived from " + mConnectedDeviceName + " SAVE THIS?");
//this type this text to the "chatwindow"
break;
Here is the splitting-chunk-problem now..
if i send under ~ 1024 I receive the correct amount of settings ant i can save this fine :-)
If i sent larger then 1024 I get first for exampel "6 settings from.." and then a new message that I recived "1 settings from.." message :-(
just for your info:
protected void recivedStringCheckFirst(String readMessage) {
String eventuellSettings = readMessage;
if (isThisASettingFile(eventuellSettings)){
//of ok..
System.out.println("incommingISAsetting :-) ");
inkommenSettings = eventuellSettings;
showDialog(); //dialog for save settings?
}
if (!isThisASettingFile(eventuellSettings)){
//not a settingsfile!
Toast.makeText(this, "try again..", Toast.LENGTH_LONG).show();
}
}
so i think it is:
case MESSAGE_READ:
is not only called if a complete file is received,
it is also called if a small chunks is received.
So I probably should place the "readFile-chunk" in a separate buffer
(i.e. mNewBufForFile += readFileChunk)
And then check the mNewBufForFile has a complete packet in it (how?). If it is done: I "save" the file message and then clear all buffer.
but how can i "split this from "Message_read", and do I "add a stopping bit" so i can check when i recive all the data? or can i do this better?
You can send as many bytes as you want. They come in in chunks smaller than the size of buffer (1024). Indeed the original code will mix all up caused by using one buffer. Change
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
to
while (true) {
try {
byte[] buffer = new byte[1024];
// Read from the InputStream
int nbytes = mmInStream.read(buffer);
Log.i(TAG, "read nbytes: " + nbytes);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, nbytes, -1, buffer)
.sendToTarget();
The data still comes in in chuncks but now you get all displayed in the rigth sequence.
As the chunck sizes -during some tests- are smaller than 1024 it makes no sense to have a bigger buffer. If you want to transfer a real file you should concatenate all together. This is a normal action using sockets.
I've been having an issue with data integrity using an RFCOMM socket over Bluetooth in Android. I don't have any issues connecting, but the data I receive is garbled and not the same as the data that is sent. The data is sent by an RS232 device over a Bluetooth adapter, which the phone connects to. There isn't a problem with the adapter as the data is properly received if I connect with a laptop.
My Bluetooth connection is handled based off of the BluetoothChat sample application found at the Android developer site (http://developer.android.com/resources/samples/BluetoothChat/index.html), with no changes. The data being sent is plain text and control characters (which are stripped out before display to the user). The specific problem I have is that some of the text is missing, some of it is repeated, etc.
The funny thing is if I connect to a computer with a terminal app and type in there, the data is transmitted fine. Additionally, if I connect to the device using the GetBlue app the data is received fine.
So I guess the issue is what does GetBlue possibly do different to handle its Bluetooth data transfer, or is there another way receive Bluetooth data over an RFCOMM socket on Android?
The fix for the solution was to create the string in the connected thread, directly after calling read() on the InputStream, and then passing the string back to the main thread for display. For whatever reason, passing the byte array between threads led to significant repetition and data loss.
Modified run() code:
public void run() {
byte[] buffer = new byte[256]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, readMessage)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
And the handler reception:
case MESSAGE_READ:
// Read in string from message, display to mainText for user
String readMessage = (String) msg.obj;
if (msg.arg1 > 0) {
mainText.append(readMessage);
}
This error is because the object reference is passed to the UI, If you copy the byte array(buffer) to another byte array it works.