0

我有一个来自 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



            }
    });


  }  

请按照我在编码行中的评论

请任何人帮助我....

4

1 回答 1

1

您看过蓝牙聊天示例应用程序吗?我认为这与您尝试做的非常接近:http: //developer.android.com/resources/samples/BluetoothChat/index.html

此外,Android 蓝牙开发人员指南包含与您提出的特定问题相关的代码示例:

//1.How to connect to a selected device code here
http://developer.android.com/guide/topics/wireless/bluetooth.html#ConnectingDevices 告诉你如何在 BluetoothServerSocket 上调用 accept() 来监听传入一侧,然后在另一侧的 BluetoothSocket 上调用 connect()。

//2.How to send the message to selected device here http://developer.android.com/guide/topics/wireless/bluetooth.html#ManagingAConnection 说明如何将您的套接字附加到 I/O 流,然后调用读/写在溪流上。

//3.如何在此处关闭与所选设备的连接您只需在 BluetoothSockets 上调用 close()。通常,您从一个线程执行此操作,这会导致蓝牙线程中的挂起操作引发 I/O 异常。

但同样,我建议通读上面列出的聊天示例和开发人员指南。

于 2012-04-06T17:18:17.357 回答