2016-11-11 95 views
0

我是相对较新的Java编程的android和不能找出如何配对两个设备,通过单击列表视图的项目,包含列表新发现的设备。通过点击一个listView项目,配对两个蓝牙设备Android

我已经创建了一个包含了一组新发现的设备ListView和这里是我的代码的一部分,用于click事件:`

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

哪里是蓝牙设备的列表?这个代码只是有一个空的适配器,就像你已经显示 –

+0

[如何通过点击Android项目中的ListView连接蓝牙设备?](http://stackoverflow.com/questions/19132160/how - 连接 - 蓝牙设备 - 点击 - 在Android项目的listview - ) –

+0

发现的蓝牙设备列表存储在“newListView”,但我不知道如何创建一个BluetoothDevice实例,以便将这个新发现的设备(通过点击newListView中的一个项目来选择)与我的BluetoothAdapter配对。 –

回答

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对象来处理配对的设备是the documentation

+0

太好了,谢谢,它对我很有用! –

+0

很高兴听到。你可以通过[接受答案]来显示你的感谢(http://stackoverflow.com/help/someone-answers) –