我有一个来自 android api 示例的蓝牙应用程序。当用户选择一个设备尝试向选定的配对设备发送消息时,我在列表中显示蓝牙配对设备,然后该消息应该传送到该设备。从这个当用户检查配对设备然后在编辑字段中输入消息然后单击发送按钮时,我想开发规范。当用户单击发送按钮时,用户可以连接到选定的设备,然后将该消息发送到该设备,然后如果交付是成功立即关闭连接。我已经实现了如下代码
ListView listDevicesFound;
ArrayAdapter<String> btArrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up the window layout
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
btArrayAdapter = new ArrayAdapter<String>(BluetoothChat.this, android.R.layout.simple_list_item_multiple_choice);
listDevicesFound.setAdapter(btArrayAdapter);
listDevicesFound.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listDevicesFound.setSelected(true);
listDevicesFound.setClickable(true);
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceBTName = device.getName();
String deviceBTAddress = device.getAddress();
btArrayAdapter.add(deviceBTName + "\n" + deviceBTAddress);
}
}
mSendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SparseBooleanArray checked = listDevicesFound.getCheckedItemPositions();
Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
Log.v("prasad", "devices.size()===>>>"+devices.size());
for (int j=0;j<=devices.size();j++)
{
System.out.println(j);
if(checked.get(j))
{
String devadd= listDevicesFound.getItemAtPosition(j).toString();
String devaddress=devadd.substring(0,devadd.length()-17);
Log.v("prasad", "address===>>>"+devaddress);
/*
*BluetoothDevice:
*Represents a remote Bluetooth device.
*A BluetoothDevice lets you create a connection with the respective device
*or query information about it, such as the name, address, class,
*and bonding state
*/
for(BluetoothDevice itDevices:devices)
{
Log.v("prasad", "itDevices.getAddress()===>>>"+itDevices.getAddress());
if(devadd.endsWith(itDevices.getAddress()))
{
Log.v("1111","Here the adderess of selected device :"+itDevices.getAddress());
}
}
}
}
//Please help on following steps
//1.How to connect to a selected device code here
String message = messageEdit.getText().toString();
//2.How to send the message to selected device here
//3.How to close the connection with selected device here
}
});
}
请按照我在编码行中的评论
请任何人帮助我....