Reputation: 539
My app is trying to connect with a Bluetooth module and Send/Receive data simultaneously. But sometimes the following error is thrown (it usually happens after a GC call):-
12-15 19:06:15.559: D/dalvikvm(22875): GC_FOR_ALLOC freed 3925K, 25% free 11400K/15096K, paused 28ms, total 28ms
12-15 19:06:15.559: E/System(22875): Uncaught exception thrown by finalizer
12-15 19:06:15.559: E/System(22875): java.io.IOException: socket not created
12-15 19:06:15.559: E/System(22875): at android.net.LocalSocketImpl.shutdownInput(LocalSocketImpl.java:392)
12-15 19:06:15.559: E/System(22875): at android.net.LocalSocket.shutdownInput(LocalSocket.java:206)
12-15 19:06:15.559: E/System(22875): at android.bluetooth.BluetoothSocket.close(BluetoothSocket.java:462)
12-15 19:06:15.559: E/System(22875): at android.bluetooth.BluetoothSocket.finalize(BluetoothSocket.java:229)
12-15 19:06:15.559: E/System(22875): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:187)
12-15 19:06:15.559: E/System(22875): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
12-15 19:06:15.559: E/System(22875): at java.lang.Thread.run(Thread.java:841)
This uncaught exception is thrown repeatedly for few number of times and as a result my app stop responding.
What can be the possible reasons for this error? I am trying to solve it but can't find the exact issue. Please help.
I am also attaching the code which I am using to connect with Bluetooth module. I am using an AsyncTask to do this..
//global variable
BluetoothSocket mBSocket;
// inside doInBackground() function
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter.isEnabled()) {
try {
for (BluetoothDevice bt : mBluetoothAdapter.getBondedDevices()) {
if (bt.getName().equalsIgnoreCase("MY_DEVICE_BT_NAME")) {
BluetoothDevice device = mBluetoothAdapter
.getRemoteDevice(bt.getAddress());
mBluetoothAdapter.cancelDiscovery(); // We have named our
// device so
// cancel search
mBSocket = device
.createRfcommSocketToServiceRecord(SPP_UUID);
if(!mBSocket.isConnected()) {
mBSocket.connect();
}
return mBSocket;
}
}
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
} else
return null;
Upvotes: 2
Views: 5638
Reputation: 539
I have modified the code to connect to my Bluetooth module, handled the exceptions properly, since then there isn't any such error I am facing as I have described in my question.
//global variable
BluetoothSocket mBSocket;
// inside doInBackground() function
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter.isEnabled()) {
try {
for(BluetoothDevice bt: mBluetoothAdapter.getBondedDevices()) {
if(bt.getName().equalsIgnoreCase(params[0])) {
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(bt.getAddress());
mBluetoothAdapter.cancelDiscovery();
mBSocket = device.createRfcommSocketToServiceRecord(SPP_UUID);
mBSocket.connect();
return mBSocket;
}
}
} catch(IOException e) {
if(mBSocket != null) {
try {
mBSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
mBSocket = null;
}
e.printStackTrace();
return null;
}
}
return null;
Upvotes: 2