我对java很陌生,目前正在做一个项目。我已经设法从我的应用程序中打开和关闭蓝牙。我还有一个包含配对设备列表的列表视图。我创建了一个“onListItemClick”方法。但是,我似乎无法弄清楚如何与其中一个配对设备建立连接。任何帮助,将不胜感激。谢谢你。
import java.util.Set;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Devices extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ArrayAdapter<String> btArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
if(pairedDevices.size()>0){
for (BluetoothDevice device :pairedDevices){
String name = device.getName();
btArrayAdapter.add(name);
}
}
setListAdapter(btArrayAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Here, I want to establish a connection with the device chosen by the user
}
}