I am not able to understand what exectly happening here with the code.
My code sometimes running fine and sometimes not.
I am creating the bluetooth connection.I am searching for the devices when the search button is clicked. It gives me the list of devices and I am displaying it on the listview. This all working fine.
Now on the listItem click, I want to pair the my device with the selected device from the list.
I have used this code to do this.
listViewDetected.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Log.i("Log", "ListItem is clicked at :"+position);
posn = position;
String str = (String) listViewDetected.getItemAtPosition(position);
Log.i("Log", "ListItem is :"+str);
bluetoothDevice=arrayListBluetoothDevices.get(position);
final BluetoothDevice device = arrayListBluetoothDevices.get(position);
Log.i("Log", "UUID string is :"+uid);
new Thread() {
public void run() {
connect(device);
};
}.start();
}
private void connect(BluetoothDevice bluetoothDevicess) {
// TODO Auto-generated method stub
Log.i("Log", "connect method after click: ");
ParcelUuid[] uuids = servicesFromDevice(bluetoothDevicess);
adapterPaired.notifyDataSetChanged();
Log.i("Log", "service method executed");
}
});
public ParcelUuid[] servicesFromDevice(BluetoothDevice device) {
try {
Log.i("Log", "service method is called ");
Class cl = Class.forName("android.bluetooth.BluetoothDevice");
Class[] par = {};
Method method = cl.getMethod("createBond", par);
Object[] args = {};
ParcelUuid[] retval = (ParcelUuid[]) method.invoke(device, args);
if(retval==null)
{
Log.i("Log", "GOT the Array as null:");
}
else
{
Log.i("Log", "GOT the Array:"+retval.length);
}
/* for(int i = 0;i<retval.length;i++)
{ Log.i("Log", "GOT : "+retval[i]);
}*/
return retval;
} catch (Exception e) {
Log.i("Log", "Inside catch of serviceFromDevice Method");
e.printStackTrace();
return null;
}
}
Sometimes it is working very fine. but sometimes its not working.
I repeat
Sometimes it is working very fine. but sometimes its not working. Not even a single line is executing of listItem clicked() method.
And I am getting this on logcat when its not working:
09-01 16:17:26.405: I/KeyInputQueue(171): Enqueueing touch event0
09-01 16:17:26.405: I/WindowManager(171): Read next event 0
09-01 16:17:26.405: I/WindowManager(171): Delivering pointer 0 > Window{4a6ea918 com.bdm/com.bdm.BluetoothDemo paused=false}
09-01 16:17:26.515: I/KeyInputQueue(171): Enqueueing touch event1
09-01 16:17:26.515: I/WindowManager(171): Read next event 1
09-01 16:17:26.515: I/WindowManager(171): Delivering pointer 1 > Window{4a6ea918 com.bdm/com.bdm.BluetoothDemo paused=false}
I got the solution of this problem. It was occurring just because of the Brodcast Receiver. I at the time i was clicking in the listItem,at the same time my receiver was getting triggerd. And in that i was calling the thread object. So that sometime, if at my thread was not called then listitem click was working fine. thanks for your interest friends. Thanks.