I am sending String "A:22.656565,76.545454#" through bluetooth.BUT At recieving time it only takes "" at first time then takes remaining String i.e "A:22.656565,76.545454#".I dont know why this occur? Any Help would be appreciated. Here is my code::
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, String socketType) {
//Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
//Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
mState = STATE_CONNECTED;
}
String received="" ;
public void run() {
//Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
while (mState == STATE_CONNECTED) {
try {
bytes = mmInStream.read(buffer);
received += new String(buffer, "UTF8");
received = received.replaceAll("\\p{C}", "");
if (received.contains("*")) {
received = received.substring(0, received.indexOf("*"));
Log.e("FOUND", ": " + received);
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, received).sendToTarget();
received = "";
buffer = new byte[1024];
}
} catch (IOException e) {
//Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
public void write(byte[] buffer) {
try {
//Log.d("Filter","Sending Data inside write3");
mmOutStream.write(buffer);
Log.e("FOUND456", ": " + buffer);
mHandler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
} catch (IOException e) {
//Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
//Log.e(TAG, "close() of connect socket failed", e);
}
}
}
Sounds like there is a small delay in receiving the data, this is not unusual. You should read data from the inputstream in a loop breaking out of the loop when the complete data is received or timeout
Related
I am new to Object Input Streams and Object Output Streams but I have to use them to send a string over Bluetooth. Whenever I try to make the connection both phones freeze and then crash. I used the debugger and the last line it stopped at before the program froze is: tmpIn = new ObjectInputStream(socket.getInputStream());
Here is my connection thread:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final ObjectInputStream mmInStream;
private final ObjectOutputStream mmOutStream;
private FileOutputStream mmFileOut = null;
public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
ObjectInputStream tmpIn = null;
ObjectOutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
//input stream
//mmFileIn = new FileInputStream("t.tmp");
tmpIn = new ObjectInputStream(socket.getInputStream());
//output stream
mmFileOut = new FileOutputStream("t.tmp");
tmpOut.flush();
tmpOut = new ObjectOutputStream(mmFileOut);
tmpOut.writeObject(socket.getOutputStream());
}catch (FileNotFoundException fnfe){
System.out.println("FileOutPutStream: "+ fnfe);
}catch (IOException ie){
System.out.print("ObjectOutputStream: " + ie);
}catch (Exception e){
System.out.print(e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
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(Constants.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;
}
}
}
/**
* Write to the connected OutStream.
*
* #param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
I read somewhere it may have to do with using .flush() on my Object Output Stream am I using that correctly?
The code in the constructor runs on the UI thread. Move it to run().
is there a way to send multiple messages with OutputStream.write(bytes[]), for example when i call twice my function to write func.write("hi"); func.write(" how are you");, I receive the message "concateneted" like this: "hi how are you", but i want two different messages, is there a way to do it without using separators in my message, i mean know when the other device receives the message, here is my code (its the android bluetooth sample):
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
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;
}
}
}
/**
* Write to the connected OutStream.
* #param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// 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);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
Call:
mmOutStream.flush()
after each message part.
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
//send what is already in 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);
}
}
From the docs:
flush()
(Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.
I am working on an Android app in which I need to transfer text from one device to another device using Bluetooth interface.
Problem is, MY BROADCAST RECEIVER IS NOT WORKING AS IT SHOULD WORK. When data is sent from one device, Broadcast listener of my receiver device doesn't function at all.
I am using below code currently:
The below code is to connect to a Bluetooth device:
protected void connect(BluetoothDevice device) {
try {
ConfigClass.isFirstBluetoothSignal=true;
Log.d(TAG,"connect bluetooth");
// Create a Socket connection: need the server's UUID number of
// registered
Method m = device.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 1);
// socket=device.createRfcommSocketToServiceRecord(MY_UUID_INSECURE);
socket.connect();
Log.d(TAG, ">>Client connectted");
inputStream = socket.getInputStream();
socket.getOutputStream();
int read = -1;
final byte[] bytes = new byte[1024];
while (true) {
synchronized (obj1) {
read = inputStream.read(bytes);
//_handler.obtainMessage(RECIEVE_MESSAGE, -1, read).sendToTarget();
Log.d(TAG, "read:" + read);
if (read > 0) {
ConfigClass.isFirstBluetoothSignal=false;
final int count = read;
String str = SamplesUtils.byteToHex(bytes, count);
// Log.d(TAG, "test1:" + str);
hex = hexString.toString();
if (hex == "") {
hexString.append("<--");
} else {
if (hex.lastIndexOf("<--") < hex.lastIndexOf("-->")) {
hexString.append("\n<--");
}
}
hexString.append(str);
hex = hexString.toString();
// Log.d(TAG, "test2:" + hex);
if (hex.length() > maxlength) {
try {
hex = hex.substring(hex.length() - maxlength,
hex.length());
hex = hex.substring(hex.indexOf(" "));
hex = "<--" + hex;
hexString = new StringBuffer();
hexString.append(hex);
}catch (NullPointerException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "e", e);
}
}
_handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), hex, Toast.LENGTH_LONG);
}
});
}
}
}
} catch (Exception e) {
ConfigClass.isFirstBluetoothSignal=false;
Log.e(TAG, ">>", e);
Toast.makeText(getBaseContext(),
getResources().getString(R.string.ioexception),
Toast.LENGTH_SHORT).show();
return;
} finally {
if (socket != null) {
try {
Log.d(TAG, ">>Client Socket Close");
socket.close();
socket = null;
// this.finish();
return;
} catch (IOException e) {
Log.e(TAG, ">>", e);
}
}
}
}
The below code is to accept incoming connection:
public class AcceptThread extends Thread{
// The local server socket
private final BluetoothServerSocket mmServerSocket;
private String mSocketType;
private BluetoothAdapter mAdapter=BluetoothAdapter.getDefaultAdapter();
public AcceptThread(boolean secure) {
BluetoothServerSocket tmp = null;
mSocketType = secure ? "Secure":"Insecure";
// Create a new listening server socket
try {
if (secure) {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID_SECURE);
} else {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID_INSECURE);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
//BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (true) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
//InputStream ins=socket.getInputStream();
/*BufferedReader r = new BufferedReader(new InputStreamReader(ins));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}*/
//System.out.println("Data============================================>>>"+line);
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (SearchDeviceActivity.this) {
ConnectedThread connectedThread=new ConnectedThread(socket, device.getAddress());
connectedThread.start();
//socket.close();
break;
}
}
}
}
}
The below code is to fetch incoming data:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
#SuppressLint({ "NewApi", "NewApi" })
public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
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
_handler.obtainMessage(SearchDeviceActivity.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
//connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
* #param buffer The bytes to write
*/
#SuppressLint("NewApi")
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
_handler.obtainMessage(SearchDeviceActivity.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
I need to show data to a TextView from a serial Bluetooth.
But when i connect my application with the serial device, it did connected but suddenly went forced closed. The logcat shows nothing so I don't know what's wrong.
This is the code when application listens the inputstream while connected:
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
//mEmulatorView.write(buffer, bytes);
mTextView.append(new String(buffer));
// Send the obtained bytes to the UI Activity
//mHandler.obtainMessage(BlueTerm.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
String a = buffer.toString();
mTextView.setText(a);
a = "";
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
and TextView mTextView = (TextView) findViewById(R.id.dataTerm);
and on the layout:
<TextView
android:id="#+id/dataTerm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
So does anyone know what went wrong?
Any answers are so helpful, thanks..
The code that finally work
On the main file, in my case named FinalSetting:
On the Activity method, declare:
//Layout View
private static TextView mTextView;
On the onCreate(Bundle savedInstanceState) method declare the textview:
mTextView = (TextView) findViewById(R.id.dataTerm);
On the Handler method:
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
//mEmulatorView.write(readBuf, msg.arg1);
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
//mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);
mTextView.setText(readMessage);
break;
On the BluetoothService.java file:
Let's just straight to the method
//This thread runs during a connection with a remote device.
//It handles all incoming and outgoing transmissions.
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
//final 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(FinalSetting.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
* #param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(FinalSetting.MESSAGE_WRITE, buffer.length, -1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
After you connect to the serial Bluetooth device, the data will show up on the TextView.
Hope this help :D
Seems like you are trying to modify mTextView in a non-UI thread which is illegal and might be the reason for FC (if not any other issue). However you can achieve this as below:
Change this:
mTextView.append(new String(buffer));
To this:
mTextView.post(new Runnable() {
#Override
public void run() {
mTextView.append(new String(buffer));
}
});
I want to share object of type Stuff which contains ( String name,address,title, ... and byte[] picture ) when connection is established after that my code hangs on objectinputstream readObject() function . No streaming occurs. Can anyone please figure out where i am doing wrong thing.
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private ObjectOutputStream oos = null;
private ObjectInputStream ois = null;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
Log.d(TAG, "create a");
InputStream tmpIn = null;
Log.d(TAG, "create b");
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
Log.d("connected thread constructor before inputstream", "");
tmpIn = socket.getInputStream();
Log.d("connected thread constructor inputstream",
tmpIn.toString());
tmpOut = socket.getOutputStream();
Log.d("connected thread constructor outputstream",
tmpOut.toString());
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
final BufferedOutputStream bufo = new BufferedOutputStream(
mmOutStream);
final BufferedInputStream bufi = new BufferedInputStream(mmInStream);
Log.d(TAG, "attempting to create OOS");
// ********* ObjectOutputStream **********
try {
oos = new ObjectOutputStream(bufo);
} catch (StreamCorruptedException e) {
Log.d(TAG, "Caught Corrupted Stream Exception");
Log.w(TAG, e);
} catch (IOException e) {
Log.d(TAG, "Caught IOException");
Log.w(TAG, e);
}
// ********* ObjectInputStream **********
Thread s = new Thread() {
public void run() {
Log.d(TAG, "attempting to create OIS");
try {
ois = new ObjectInputStream(bufi);
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "completed OIS");
if (ois == null) {
Log.d(TAG, "OIS is null");
}
}
};
s.start();
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
My code hang at this point , never move forward .
`// Keep listening to the InputStream while connected
while (true) {
try {
Log.d("Connected thread run ", "start while");
try {
Stuff obj_rcv = (Stuff) ois.readObject();
Log.d("BTS", "rcv object " + obj_rcv.getName());
Message msg2 = mHandler
.obtainMessage(RemoteBusinessCard.MESSAGE_READ);
Bundle bundle = new Bundle();
bundle.putSerializable("person", obj_rcv);
msg2.setData(bundle);
mHandler.sendMessage(msg2);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
*
* #param buffer
* The bytes to write
*/
public void write(Stuff object) {
try {
Log.d("BTS", "inside write before" + object.getName());
oos.writeObject(object);
Log.d("BTS", "inside write after" + object.getName());
oos.flush();
oos.close();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}`
When you create an ObjectOutputStream you must ensure that the ObjectInputStream at the other end of the socket is also appropriately created because the outputStream always sends a confirmation packet to the inputStream and blocks until it gets an answer.
this is not the way java works.
You MUST create two separate threads for input and output streams for your logic to work.
outStream = new ObjectOutputStream(socket.getOutputStream());
outStream.flush();
inStream = new ObjectInputStream(socket.getInputStream());
create outputstream first, flush and inputstream both in server and client