[英]Sending device address to ConnectThread(BluetoothDevice device) in android -bluetooth
I have button called scan, that scans the available blue-tooth devices and make a list view. 我有一个名为scan的按钮,它可以扫描可用的蓝牙设备并制作一个列表视图。 When i press on some device in that device list , 'm calling ConnectThread(BluetoothDevice device) but how to send the clicked device address to this.'m using below code.In this 'm able to getting name but how to get the address.
当我在该设备列表中的某个设备上按时,“正在调用ConnectThread(BluetoothDevice设备),但如何将单击的设备地址发送到此。”使用下面的代码。在此过程中,我可以获取名称,但如何获取地址。
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mBluetoothAdapter.cancelDiscovery();
// Toast.makeText(getApplicationContext(), "clicked device", 2).show();
String address = mArrayAdapter.get(position);
device = mBluetoothAdapter.getRemoteDevice(address);
new ConnectThread(device);
}
});
ConnectThread code: ConnectThread代码:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
Toast.makeText(getApplicationContext(), "device: " + device, 2).show();
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
// manageConnectedSocket(mmSocket);
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
I found answer for this question. 我找到了这个问题的答案。 Used below code when item i clicked in list view.
当我在列表视图中单击项目时,在下面的代码中使用。
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mBluetoothAdapter.cancelDiscovery();
String info = mArrayAdapter.get(position);
String address = info.substring(info.length() - 17);
device = mBluetoothAdapter.getRemoteDevice(address);
new ConnectThread(device);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.