2016-11-11 8 views
0

私はAndroidでプログラミングするのが比較的新しく、リストの項目をクリックして2つのデバイスをペアにする方法を理解できません新しく発見されたデバイス。リストビューの項目をクリックして2つのBluetoothデバイスをペア設定すると、

は、私はすでに、新たに発見されたデバイスのセットを含むリストビューを作成し、ここでのクリックイベントのために私のコードの一部ですしました: `

public class MainActivity extends AppCompatActivity { 

ListView newListView; // listView containing newly discovered devices 
ArrayAdapter<String> mNewDevicesArrayAdapter; 
BluetoothAdapter mBluetoothAdapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 


    /* Variables definition */ 

    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 
    newListView = (ListView) findViewById(R.id.new_lv); 

    // New Devices List View item click 
    newListView.setClickable(true); 

    newListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     // some code must go here, but I can't figure out which one 


     } 
    }); 

    } 

おかげで、事前にあなたの助けのために!

+0

the documentationにありますか?このコードはあなたが表示している限り、空のアダプタを持っています –

+0

[Androidのlistviewの項目をクリックしてBluetoothデバイスを接続する方法は?](http://stackoverflow.com/questions/19132160/howブルートゥースデバイスをクリックしてリストビューインアンドロイドに接続する) –

+0

発見されたBluetoothデバイスのリストは「newListView」に保存されていますが、この新たに発見されたデバイス(newListViewからアイテムをクリックして選択したもの)とBluetoothAdapterをペアにするために、BluetoothDeviceのインスタンスを作成します。 –

答えて

0

アダプターに文字列のリストがありますか?

あなたはペアリングデバイスを処理するための仕様のBluetoothAdapter

newListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     String address = mDevicesAdatper.getItem(position); 
     BluetoothDevice btDevice = mBluetoothAdapter.getRemoteDevice(address); 

     // TODO: Pair 

    } 
}); 

くらいからBluetoothDeviceオブジェクトを取得することができますは、Bluetoothデバイスのリストがある

+0

偉大な、ありがとう、それは私のために働いた! –

+0

喜んで聞いてください。あなたは[回答を受け入れる](http://stackoverflow.com/help/someone-answers)であなたの感謝を示すことができます –

関連する問題