小编Sre*_*yas的帖子

安卓蓝牙后台监听器

我正在开发一个带有蓝牙聊天功能的 android 应用程序。我已经成功地用两部手机实现了蓝牙聊天。但我的问题是,如果我从聊天活动更改为下一个活动,连接就会丢失,那么我无法从第二个活动发送消息。如何保持连接?
那就是我想通过我的应用程序保持联系。每当用户按下退出按钮时,只有连接才能断开连接。我想从一个活动发送消息并从另一个活动接收这就是我想要的。我无法使用我的代码创建后台服务。
谁能帮我拆分我的代码?如果我从一部手机收到一条消息,那么我想处理该消息并发回结果,该处理将在下一个活动中进行,这是我的应用程序的工作。

  public class BluetoothTexting extends Activity {

  private static int DISCOVERY_REQUEST = 1;

  private Handler handler = new Handler();

  private ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>();
    private ArrayAdapter<BluetoothDevice> aa; 
  private ListView list;

  private BluetoothAdapter bluetooth;
  private BluetoothSocket socket;
  private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    configureBluetooth();


    setupListView();    


    setupSearchButton();


    setupListenButton();
    }

     private void configureBluetooth() {
     bluetooth = BluetoothAdapter.getDefaultAdapter();
     }

      private void setupListenButton() {
      Button listenButton = (Button)findViewById(R.id.button_listen);
      listenButton.setOnClickListener(new OnClickListener() {
      public void …
Run Code Online (Sandbox Code Playgroud)

android bluetooth background-service android-bluetooth

5
推荐指数
1
解决办法
1万
查看次数