【问题标题】:How to connect two Bluetooth SPP devices within the same app?如何在同一个应用程序中连接两个蓝牙 SPP 设备?
【发布时间】:2018-10-14 06:39:46
【问题描述】:

我想知道,有人知道如何在同一个应用程序中连接两个蓝牙 SPP 设备吗?我查看了BluetoothChat 示例,但是,它没有提供有关如何连接到两个蓝牙 SPP 设备的任何信息。我似乎在其他地方也找不到太多信息。

【问题讨论】:

    标签: android bluetooth android-bluetooth rfcomm spp


    【解决方案1】:

    假设我们有两个蓝牙设备 B 和 C。要连接它们,我们需要

    1. 蓝牙插座,每个设备一个。

    2. 用于发送消息的输入和输出流。

    连接参数:{蓝牙设备(MAC地址),UUID}

    要拥有多个连接,我们必须创建这些连接参数专用于一个连接。

    【讨论】:

      【解决方案2】:

      此线程在我的服务类中。
      首先,绑定服务并像这样在服务类中创建一个方法, 调用此方法并传递蓝牙mac地址。它将在后台连接。对于第二个设备也遵循类似的程序。

          public synchronized void connectToDevice(String macAddress){
          BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(macAddress);
          if (mState == STATE_CONNECTING){
              if (mConnectThread != null){
                  mConnectThread.cancel();
                  mConnectThread = null;
              }
          }
          if (mConnectedThread != null){
              mConnectedThread.cancel();
              mConnectedThread = null;
          }
          mConnectThread = new ConnectBtThread(device);
          toast("connecting");
          mConnectThread.start();
          setState(STATE_CONNECTING);
      }
      

      这里我正在创建 Thread 类以在后台连接和运行

          private class ConnectBtThread extends Thread{
          private final BluetoothSocket mSocket;
          private final BluetoothDevice mDevice;
      
          public ConnectBtThread(BluetoothDevice device){
              mDevice = device;
              BluetoothSocket socket = null;
              try {
                  socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString(B_UUID));
              } catch (IOException e) {
                  e.printStackTrace();
              }
              mSocket = socket;
      
          }
      
          @Override
          public void run() {
              if (mBluetoothAdapter.isDiscovering()){
                  mBluetoothAdapter.cancelDiscovery();
              }
      
              try {
                  mSocket.connect();
                  Log.d("service","Bluetooth one running (connected)");
                  SharedPreferences pre = getSharedPreferences("BT_NAME",0);
                  pre.edit().putString("bluetooth_connected",mDevice.getName()).apply();
                   int i = 0;
                  Log.d("service","one + " +i++);
      
              } catch (IOException e) {
      
                  try {
                      mSocket.close();
                      Log.d("service","connect thread run method ( close function)");
                  } catch (IOException e1) {
                      e1.printStackTrace();
                  }
                  e.printStackTrace();
              }
              connected(mSocket);
      
          }
      
          public void cancel(){
      
              try {
                  mSocket.close();
                  //Toast.makeText(getApplicationContext(),"Failed to connect one",Toast.LENGTH_SHORT).show();
                  Log.d("service","connect thread cancel method");
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      

      与此类似,创建一个方法和线程类以使两个蓝牙设备保持在连接状态。 我遵循了这一点,它对我来说工作正常。

      【讨论】:

        猜你喜欢
        • 2020-04-30
        • 1970-01-01
        • 1970-01-01
        • 2014-12-02
        • 1970-01-01
        • 2015-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多