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

Bluetooth 자동 연결(mBluetoothGatt Null인 이유)

0 추천

bluno basic memo의 안드로이드 프로그램을 분석하여 한번 연결되었던 것은 자동으로 다시 연결되게 하려고 합니다. (원래 // Previously connected device.  Try to reconnect 코드는 주석이였음)

 

주석을 풀고도 안되는데 조건문이

 if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null)

입니다.

 

09-24 02:21:54.695  30502-30502/   after  before address  D0:39:72:C4:DE:2B  null (연결 전)

09-24 02:21:54.705  30502-30502/     before address  D0:39:72:C4:DE:2B  android.bluetooth.BluetoothGatt@423b8c98  (바로 연결 직후)

09-24 02:21:58.905  30502-30502    after address  D0:39:72:C4:DE:2B  android.bluetooth.BluetoothGatt@423e5230

--------------------------

연결 끊고 재 연결시도

-------------------------

09-24 02:21:58.895  30502-30502        before address  D0:39:72:C4:DE:2B  null ( BluetoothGatt = NULL이 됨)

 

한번 연결 후 재 연결을 시도할 때 확인해보니까 mBluetoothDeviceAddress랑  address.equals은 NULL이 아닌데 mBluetoothGatt이 Null이라 연결이 되지 않습니다.

mBluetoothGatt이 의미하고 있는 것이 무엇인지 왜 초기화 되는지 궁금합니다

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

      // Previously connected device.  Try to reconnect.
      System.out.println("before address  "+mBluetoothDeviceAddress +"  "+ mBluetoothGatt);

      if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
              && mBluetoothGatt != null) {
          Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
          if (mBluetoothGatt.connect()) {
           System.out.println("mBluetoothGatt connect");
              mConnectionState = STATE_CONNECTING;
              return true;
          } else {
           System.out.println("mBluetoothGatt else connect");
              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.
      System.out.println("device.connectGatt connect");
synchronized(this)
{
   mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
}
      Log.d(TAG, "Trying to create a new connection.");
      mBluetoothDeviceAddress = address;
      System.out.println("after address  "+mBluetoothDeviceAddress +"  "+ mBluetoothGatt);

      mConnectionState = STATE_CONNECTING;
      return true;
  }

 

 

 

눈온다다다 (730 포인트) 님이 2015년 9월 24일 질문

답변 달기

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