Reputation: 121
I am trying to read data from a serial bluetooth stream in my android application. The data is send to a handler to display it. It works fine for a few minutes and then it stops showing data (The app keeps running however). I think it has to do with memory leaks in my handler but I don't know how to solve it..
This is where I found the code
I will be so happy if someone can help me. Thanks in advance
public class MainActivity extends ActionBarActivity {
static BluetoothAdapter mBluetoothAdapter = null;
static Handler mHandler = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Does the device support bluetooth?
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Device does not support Bluetooth",Toast.LENGTH_LONG).show();
}
//Turn on Bluetooth if disabled
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
//Get the Bluetooth module device
BluetoothDevice mDevice = null;
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if(device.getName().equals("HC-06")) {
mDevice = device;
}
}
}
if(mDevice == null){
Toast.makeText(this,"Device not found",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this,"connected to " + mDevice.getName(),Toast.LENGTH_LONG).show();
ConnectThread mConnectThread = new ConnectThread(mDevice);
mConnectThread.start();
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
byte[] writeBuf = (byte[]) msg.obj;
int begin = (int)msg.arg1;
int end = (int)msg.arg2;
switch(msg.what) {
case 1:
String writeMessage = new String(writeBuf);
writeMessage = writeMessage.substring(begin, end);
ScrollView scrollView1 = (ScrollView) findViewById(R.id.scroll);
TextView textView1 = (TextView) findViewById(R.id.statusText);
textView1.append(writeMessage + "\n");
scrollView1.fullScroll(View.FOCUS_DOWN);
break;
}
}
};
}
}
Upvotes: 1
Views: 469
Reputation: 121
I've found a solution, it works when I replace the run method in ConnectedThread with this:
public void run() {
byte[] buffer = new byte[1024];
int begin = 0;
int byteOffset = 0;
int byteCount;
while (true) {
try {
byteCount = buffer.length - byteOffset;
byteOffset += mmInStream.read(buffer, byteOffset, byteCount);
for(int i = begin; i < byteOffset; i++) {
if(buffer[i] == "#".getBytes()[0]) {
mHandler.obtainMessage(1, begin, i, buffer).sendToTarget();
begin = i + 1;
if(i == byteOffset - 1) {
byteOffset = 0;
begin = 0;
break;
}
}
}
if(byteOffset == 1024){
begin = 0;
byteOffset = 0;
}
}
catch (IOException e) {
e.printStackTrace();
break;
}
}
}
Upvotes: 0
Reputation: 146
It's not memory leaks, in handleMessage() you should perform lightweight operations like setting text to textview.
BUT declare your scrollView1 and textview 1 as global params, just to avoit operation findViewById each time, it should help.
Also you passing some data but after generating string you crop it, so you don't need full data, isn't it better to perform that in you CommnadThread and pass exactly data what you need to show.
Upvotes: 1