1

我正在为新的车辆安全应用程序编写程序。该应用程序允许用户通过他的手机应用程序控制锁定/解锁操作。假设用户的手机蓝牙首先关闭。如果是这种情况,当他打开应用程序时,手机蓝牙适配器应该会自动打开,并应该与固定在车辆上的蓝牙模块连接。根据我所做的代码,手机蓝牙适配器的编程启用工作正常。但是与车辆 BT 模块的连接不会发生。

但是如果用户在手机蓝牙适配器已经打开的情况下打开应用程序,那么车辆和手机之间的连接会自动建立。

我需要知道为什么以编程方式打开 BT 适配器时连接不会发生。

注意 - 手机和车载 BT 模块已配对。蓝牙模块的mac地址在编码中是硬编码的。编码如下。我只粘贴了必要的部分。我希望了解和解决我的问题所需的每一个都在这里。我发布代码的方式非常混乱。对于那个很抱歉。希望很清楚。我是新手。

    private static final UUID MY_UUID =
      UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  // Insert bluetooth devices MAC address
  private static String address = "00:19:5D:EF:03:79";


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

  setContentView(R.layout.main); 
  btAdapter = BluetoothAdapter.getDefaultAdapter();
    btAdapter.enable();


  @Override
  public void onResume() {
    super.onResume();

    btAdapter.enable();

     // Set up a pointer to the remote node using it's address.
    BluetoothDevice device = btAdapter.getRemoteDevice(address);

    // Two things are needed to make a connection:
    //   A MAC address, which we got above.
    //   A Service ID or UUID.  In this case we are using the
    //     UUID for SPP.


    try {
      btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
      errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
    }

    // Make sure Discovery isn't going on when you attempt to connect and pass your message.

    btAdapter.cancelDiscovery();

    // Establish the connection.  This will block until it connects.

    try {
      btSocket.connect();

    } catch (IOException e) {
      try {
        btSocket.close();
      } catch (IOException e2) {
        errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
      }
    }

    // Create a data stream so we can talk to server.

    try {
      outStream = btSocket.getOutputStream();
    } catch (IOException e) {
      errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
    }
  }
4

1 回答 1

1

可能存在时间问题,onCreate 和 onResume 的调用顺序很短。在BT未启用的情况下,可能会在BT服务上线之前调用onResume中的代码。

我的建议:尝试通过将代码放在 Runnable 中来延迟启动几秒钟。

private Handler mHandler = new Handler();

public void onCreate() {

     [...]

     mHandler.postDelayed(new Runnable() {
         @Override
         public void run() {
             btAdapter.enable();
              // Set up a pointer to the remote node using it's address.
             BluetoothDevice device = btAdapter.getRemoteDevice(address);
             // Two things are needed to make a connection:
             //   A MAC address, which we got above.
             //   A Service ID or UUID.  In this case we are using the
             //     UUID for SPP.
             try {
                 btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
             } catch (IOException e) {
                 errorExit("Fatal Error", "In onResume() and socket create failed: " +                  e.getMessage() + ".");                
             }

             // Make sure Discovery isn't going on when you attempt to connect and pass your message.

             btAdapter.cancelDiscovery();

             // Establish the connection.  This will block until it connects.

             try {
               btSocket.connect();

             } catch (IOException e) {
               try {
                 btSocket.close();
               } catch (IOException e2) {
                 errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
               }
             }

             // Create a data stream so we can talk to server.

             try {
               outStream = btSocket.getOutputStream();
             } catch (IOException e) {
               errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
             }
     }, 5000); // 5 second delay

     [...]

警告:如果您在启动后立即退出应用程序,这将非常糟糕。将runnable放入成员变量中,在onDestroy()中调用mHandler.removeCallback(Runnable)。

于 2013-09-04T07:19:01.707 回答