我正在开发一个Android应用程序,它在其中接收配对设备的列表。我想从可用设备列表中开始与用户点击的设备建立连接。因此,我向用户显示配对设备列表,然后通过单击其中一个设备,应用程序应该开始从所选设备收听和接收CSV格式的数据。
我已经对这个问题进行了大量的搜索,但我越来越感到困惑。我应该在onItemClickListener
方法中做些什么来获得我想要的东西?
这是我的代码:
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String info = ((TextView)view).getText().toString();
String address = info.substring(info.length() - 17);
BluetoothDevice device = myBluetoothAdapter.getRemoteDevice(address);
try {
Log.d("pairDevice()", "Start Pairing...");
Class cl = Class.forName("android.bluetooth.BluetoothDevice");
Class[] par = {};
Method method = cl.getMethod("createBond", par);
Object[] args = {};
method.invoke(device, args);
} catch (Exception e) {
}
BluetoothSocket tmp = null;
BluetoothSocket mmSocket = null;
final UUID uuid = UUID.fromString("fc5ffc49-00e3-4c8b-9cf1-6b72aad1001a");
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(uuid);
Method m;
try {
m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mmSocket = tmp;
}
}
任何帮助都将不胜感激。