0

私はBLEとAndroid携帯との間の接続のための2つのオプションがあります。私は両方の方法をテストしたBluetooth Low EnergyとAndroid phoneの間に安定した接続を可能にするモードはどれですか?

connectGatt(this, false, mGattCallback); // If We want to directly connect to the device 
connectGatt(this, true, mGattCallback); // If We want to automatically connect (in background) to the device 

を:最初のものは、より高速の接続であるが、それは、バックグラウンドで再接続を許可しません。しかし、この質問では、私は安定した接続を考えています。どちらが2つの方法でより安定していますか?より安定した接続が必要な場合は、私にそれを行う方法を提案できますか?ありがとうございすべて

これは私の完全なコード

public boolean connect(final String address) { 
     if (mBluetoothAdapter == null || address == null) { 
      Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); 
      return false; 
     } 

     // Previously connected device. Try to reconnect. 
     if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) 
       && mBluetoothGatt != null) { 
      Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); 
      if (mBluetoothGatt.connect()) { 
       Log.d(TAG, "Call connect function"); 
       mConnectionState = STATE_CONNECTING; 
       return true; 
      } else { 
       return false; 
      } 
     } 

     final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 
     if (device == null) { 
      Log.w(TAG, "Device not found. Unable to connect."); 
      return false; 
     } 
     // We want to directly connect to the device, so we are setting the autoConnect 
     // parameter to false. 

     //mBluetoothGatt = device.connectGatt(this, false, mGattCallback); 
     new Handler(getMainLooper()).post(new Runnable() { 
      @Override 
      public void run() { 
       if (device != null) { 
        Log.d(TAG, "Connect connectGatt"); 
        mBluetoothGatt = device.connectGatt(getApplicationContext(), true, mGattCallback); 
       } 
      } 
     }); 

     mBluetoothDeviceAddress = address; 
     mConnectionState = STATE_CONNECTING; 
     return true; 
    } 

答えて

0

私は真の自動接続=と一緒に行きたいです。接続が切断された場合は再接続するために何もする必要はなく、接続を維持するために永遠に再試行します。ただし、Bluetoothが再起動されたときに、Bluetoothの状態の変更をリッスンして再接続する必要があります。このバグにご注意ください:https://code.google.com/p/android/issues/detail?id=69834

しかし、AndroidのコードとBluetoothコントローラ内の多くのBluetoothチップメーカーのコードのバグにより、100%安定したBLE接続が確立されることはありません。

+0

ありがとうございました。しかし、私は両方のオプションでテストし、autoConnect = falseはより安定して見えます。私はそれが正しいかどうか分からない。 – Jame

+0

もっと安定してどういう意味があるのか​​詳しく説明できますか? 1つの接続が起動していて、その特定の接続の安定性は、autoConnectが使用されたかどうかに依存しません。 – Emil

+0

遅れて返答して申し訳ありません。安定した接続とは、切断機能を呼び出すまで接続を維持するという意味です。しかし、私の場合は、BLEデバイスを約3秒間接続しましたが、自動的に切断されます(ちょっとだけ) – Jame

関連する問題