• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps UI hangs while sending data on bluetooth.

tama

Lurker
Aug 20, 2013
1
0
Hello All,

This is my first post to this forum. I am new to android development. I am trying to write a client application which talk to server application via bluetooth. Server application is running on embedded platfrom.

I referred bluetoothchat example for bluetooth data transfer. In my application I have two activities, Main activity shows list of bluetooth devices and when a user clicks on one of the devices it will open a new activity. Then it will open a bluetooth socket start thread to connect to server. I have a message handler which talks to the connected thread.

my codes looks like this

[HIGH]
public class ConnectActivity extends Activity {

// Unique UUID for this application
public static final UUID MY_UUID_INSECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

String address;
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
protected static final int RECIEVE_MESSAGE = 2; // Status for Handler
public BluetoothSocket mmSocket;
public InputStream mmInStream;
public OutputStream mmOutStream;
String tag = "debugging";

private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg)
{
// TODO Auto-generated method stub
Log.i(tag, "in handler");
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// DO something
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
//Toast.makeText(getApplicationContext(), "CONNECT", 0).show();
String s = "successfully connected";
//connectedThread.write(s.getBytes());
connectedThread.write(s.getBytes());
Log.i(tag, "connected");
break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
//String string = new String(readBuf, 0, msg.arg1);
Toast.makeText(getApplicationContext(), "Test", 0).show();
// Create the text view
//TextView textView = (TextView)findViewById(R.id.rcvedMsg);
//textView.setTextSize(40);
//textView.setText(string);
break;
case RECIEVE_MESSAGE:
byte[] readmsgBuf = (byte[])msg.obj;
String string = new String(readmsgBuf, 0, msg.arg1);
Toast.makeText(getApplicationContext(), "Test", 0).show();
// Create the text view
//TextView textView = (TextView)findViewById(R.id.rcvedMsg);
//textView.setTextSize(40);
//textView.setText(string);
break;
}
}
};
[/HIGH]

[HIGH] private class ConnectedThread extends Thread
{
public ConnectedThread(BluetoothSocket socket)
{
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

public void run() {
byte[] buffer ; // 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
buffer = new byte[1024];
bytes = mmInStream.read(buffer);
Log.d("MR", "input stream :"+(new String(buffer)));
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}

/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
//a delay of 20ms occurs after each flush...
mmOutStream.write(bytes);
mmOutStream.flush();
} catch (IOException e) { }
}

/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
} [/HIGH]

method to write to data to bluetooth on pressing a button.

[HIGH] public void ledOff(View v ) throws IOException
{
//Toast.makeText(getApplicationContext(),"LED OFF", 0).show();
/* if (mmSocket != null)
try {
mmSocket.getOutputStream().write('f');
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
if(mmOutStream != null)
{
mmOutStream.write('f');
}
}[/HIGH]

Some times my UI hangs when I click a button, it takes ages to respond back. In my method for ledOn I call bluetooth input stream. Is this correct method to do it.

Can some one please share some views on this.
Thanks
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones