Android MessagePassing通过蓝牙

时间:2013-09-30 06:23:25

标签: android bluetooth android-bluetooth

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

      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;
}

}

1 个答案:

答案 0 :(得分: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权限,如蓝牙和管理员是重要的。并且上帝禁止它是一个双向通信应用程序,传递双向消息,然后你必须做同样的编码来处理服务器以及两个应用程序中的客户端。

相关问题