我无法理解代码到底发生了什么。我的代码有时运行良好,有时不能。
我正在创建蓝牙连接。单击搜索按钮时,我正在搜索设备。它给了我设备列表,我在列表视图上显示它。这一切都很好。
现在单击 listItem,我想将我的设备与列表中的选定设备配对。我已经使用此代码来执行此操作。
   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;
    }
}
有时它工作得很好。但有时它不起作用。
我重复
有时它工作得很好。但有时它不起作用。甚至没有一行正在执行 listItem clicked() 方法。
当它不工作时,我在 logcat 上得到它:
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}