我有一个来自android api sample.z的蓝牙应用程序,当用户选择一个设备时,在列表中显示蓝牙配对设备尝试向选定的配对设备发送消息,然后该消息应该交付给该设备。我想开发一个这个规范,当用户检查配对的设备,然后在编辑字段中输入消息,然后点击发送按钮。当用户点击发送按钮,然后用户可以连接到选定的设备,然后发送该消息到该设备,然后如果递送成功,立即关闭连接。 我已经实现的代码如下如何连接设备发送消息,然后关闭Android应用程序中的蓝牙连接?
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
}
});
}
请按照我的意见在编码线
请任何机构帮助我....