0

我正在开发一个通过蓝牙传递消息的应用程序。我想将一条消息从一台设备传递到另一台设备(设备已经配对)我能够显示配对的设备。但我不知道如何连接两个设备。任何人都可以告诉我应该遵循哪些步骤。如何在两部手机之间建立连接?

      public class MainActivity extends Activity {
  TextView textview1;
  private static final int REQUEST_ENABLE_BT = 1;
  BluetoothAdapter btAdapter; 

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textview1 = (TextView) findViewById(R.id.textView1);

    // Getting the Bluetooth adapter
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    textview1.append("\nAdapter: " + btAdapter);

    CheckBluetoothState();
  }

  /* It is called when an activity completes.*/
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_ENABLE_BT) {
      CheckBluetoothState();
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  private void CheckBluetoothState() {
    // Checks for the Bluetooth support and then makes sure it is turned on
    // If it isn't turned on, request to turn it on
    // List paired devices
    if(btAdapter==null) { 
      textview1.append("\nBluetooth NOT supported. Aborting.");
      return;
    } else {
      if (btAdapter.isEnabled()) {
        textview1.append("\nBluetooth is enabled...");

        // Listing paired devices
        textview1.append("\nPaired Devices are:");
        Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
        for (BluetoothDevice device : devices) {
          textview1.append("\n  Device: " + device.getName() + ", " + device);
        }
      } else {
        //Prompt user to turn on Bluetooth
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
      }
    }
  }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

4

1 回答 1

0

而不是使用textView,使用 ListView 并items以与添加它们相同的方式添加到它textView

//declaration in class
ListView  lview;
ArrayAdapter<String> listAdapter;

//in onCreate()

lview  = (ListView) findViewById(R.id.listPairedDev);
lview.setOnItemClickListener(this);

///////here it gets added to list
ArrayOfDevices = btAdapter.getBondedDevices();
                    if(ArrayOfDevices.size()>0)//paired dev more than 0
                    {
                        for(BluetoothDevice device: ArrayOfDevices)
                        {
                            listAdapter.add(device.getName()+ "\n" +device.getAddress());

                        }
                    }

并阅读有关如何添加匿名onClickListener或类型的动作侦听器的信息。这具有如下所示的方法:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {


//Click event on individual item of list.



}

当你设置点击事件时,设置一个服务器来监听你的消息发送尖叫。那将是一个服务器(设置为服务器,不要照原样接受我的话)。
在此之前,请确保您的应用程序已使用线程连接到服务器。您可以阅读有关 android 线程的更多信息。连接到服务器(其他应用程序充当服务器),清单文件中的 android 权限,如蓝牙和管理员很重要。上帝保佑,如果它是双向通信应用程序,传递双向消息,那么您必须执行相同的编码来处理两个应用程序中的服务器和客户端。

于 2013-10-10T20:46:19.260 回答