2012-04-06 77 views
0

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



      } 
    }); 


    } 

请按照我的意见在编码线

请任何机构帮助我....

回答

1

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

另外,Android的蓝牙开发者指南具有与你问的具体问题代码示例:

//1.How连接到这里选择的设备代码
http://developer.android.com/guide/topics/wireless/bluetooth.html#ConnectingDevices 告诉您如何调用BluetoothServerSocket上的accept()来监听一侧的传入,然后调用另一侧的BluetoothSocket上的connect()。

//2.How将消息发送到此处所选设备 http://developer.android.com/guide/topics/wireless/bluetooth.html#ManagingAConnection 解释了有关你的插座连接到I/O流,然后调用流的读/写。

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

但是,我仍然建议您阅读聊天示例和开发人员指南,如上所列。

相关问题