以编程方式连接到蓝牙设备

时间:2013-09-04 06:50:37

标签: android bluetooth

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

但是如果用户在电话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() + ".");
    }
  }

1 个答案:

答案 0 :(得分: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)。