마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

BluetoothLE 재연결 질문드립니다!

0 추천

안녕하세요.

BluetoothLE 관련해서 개발을 하고 있습니다.

BluetoothLE는 원래 이렇게 잘끊어지는 것인지 제 코드가 잘못되었는지 모르겠지만 엄청 잘끊어져서 끊어졌을 때 재연결을 해주어야겠다고 생각했습니다.

전체적으로는 아래처럼 되어있습니다.

0. BluetoothAdapter mBtAdapter = mBluetoothManager.getAdapter();

1. 블루투스 스캔 ->  BluetoothAdapter.LeScanCallback에서 받아오는 파라미터 중 device가 있었습니다.

2. BluetoothDevice mBtDevice = mBtAdapter.getRemoteDevice(device.getAddress());

3. BluetoothGatt mBluetoothGatt = device.connectGatt(this, false, mGattCallbacks);

4. mBluetoothGatt.connect();

5. mGattCallbacks에서 확인.

 

그리고 2~4는 서비스에서 동작되도록 되어있습니다.

 

제가 해당 서비스를 시작시켜준 액티비티에서 연결이 끊긴것을 확인하면 해당 서비스의 connect()함수를 실행시킴으로써 이문제를 해결해보려고 했는데 잘안되는것같습니다.

참고로 해당 서비스의 connect함수는 아래와 같습니다.

public boolean connect(final String address) {
   if (mBtAdapter == null || address == null) {
      Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
      return false;
   }
   final BluetoothDevice device = mBtAdapter.getRemoteDevice(address);
       //this.mDevicePrefs = new PreferenceWR(device.getAddress(),this.getApplicationContext());
   int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
   if (connectionState == BluetoothProfile.STATE_DISCONNECTED) {
      // Previously connected device. Try to reconnect.
      if (mBluetoothDeviceAddress != null
          && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
          Log.d(TAG, "Re-use GATT connection");
         if (mBluetoothGatt.connect()) {
            return true;
         } else {
             Log.w(TAG, "GATT re-connect failed.");
            return false;
         }
      }

      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.
      Log.d(TAG, "Create a new GATT connection.");
      mBluetoothGatt = device.connectGatt(this, false, mGattCallbacks);
      mBluetoothDeviceAddress = address;
   } else {
      Log.w(TAG, "Attempt to connect in state: " + connectionState);
      return false;
   }
   return true;
}

로그는 아래와 같습니다.

01-17 13:49:08.821 4766-8033/com.sensortest.coreonetech.coreoneremedial D/MotionExerciseCont: sensor is disconnected~~~~~

01-17 13:49:08.821 4766-8033/com.sensortest.coreonetech.coreoneremedial D/MoveSensor: address : B0:B4:48:C2:7B:87

01-17 13:49:08.821 4766-8033/com.sensortest.coreonetech.coreoneremedial D/BluetoothManager: getConnectionState()

01-17 13:49:08.821 4766-8033/com.sensortest.coreonetech.coreoneremedial D/BluetoothManager: getConnectedDevices

01-17 13:49:08.831 4766-8033/com.sensortest.coreonetech.coreoneremedial D/BluetoothLeAService: Re-use GATT connection

01-17 13:49:08.831 4766-8033/com.sensortest.coreonetech.coreoneremedial D/BluetoothManager: getConnectionState()

01-17 13:49:08.831 4766-8033/com.sensortest.coreonetech.coreoneremedial D/BluetoothManager: getConnectedDevices

01-17 13:49:08.831 4766-4777/com.sensortest.coreonetech.coreoneremedial D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=6 device=B0:B4:48:C2:7B:87

01-17 13:49:08.831 4766-8033/com.sensortest.coreonetech.coreoneremedial D/MoveSensor: IsConnect : false

01-17 13:49:08.831 4766-4777/com.sensortest.coreonetech.coreoneremedial D/BluetoothLeAService: onConnectionStateChange (B0:B4:48:C2:7B:87) 0 status: 133

01-17 13:49:08.831 4766-4777/com.sensortest.coreonetech.coreoneremedial D/BluetoothLeAService: broadcastUpdate

01-17 13:49:08.831 4766-4777/com.sensortest.coreonetech.coreoneremedial D/BluetoothLeAService: action : com.coreonetech.rehabilitation.ble.common.ACTION_GATT_DISCONNECTED, address : B0:B4:48:C2:7B:87, status : 133

 

MotionExerciseCont에서 sensor is disconnected~~~~~는 제가 액티비티에서 블루투스가 끊긴것을 알게되면 임의로 뛰운 로그입니다.

그다음에 서비스.connect()를 써서 연결을 시도하였습니다.

그뒤에 나오는 로그를 보니까 붙이자마자 DISCONNECTED를 브로드캐스트로 받은것 같습니다.

disconnect되면 계속 connect()하니까 계속 저런 로그의 반복입니다.

제가 재연결하려는 방법이 잘못된것인가요?

고수님들 도와주세요!

익명사용자 님이 2017년 1월 17일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...