3

ここでコードで正確に何が起こっているのか理解できません。私のコードは時々うまく動くこともあれば、うまくいかないこともあります。

Bluetooth接続を作成しています。検索ボタンをクリックすると、デバイスを検索しています。デバイスのリストが表示され、リストビューに表示されます。これはすべて正常に機能しています。

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() メソッドを 1 行も実行していません。

そして、動作していないときに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}
4

1 に答える 1

0

この問題の解決策を得ました。Brodcast Receiver が原因で発生していました。私はlistItemをクリックしていたとき、同時にレシーバーがトリガーされていました。その中で、スレッドオブジェクトを呼び出していました。そのため、私のスレッドで呼び出されなかった場合、listitem のクリックは正常に機能していました。興味のある友達に感謝します。ありがとう。

于 2012-09-03T04:34:35.247 に答える