본문 바로가기
프로그램 개발/안드로이드

[안드로이드] 블루투스 서비스 에러 / Bluetooth Service Error

by 코딩히어로 2020. 3. 25.

블루투스 BLE 어플 개발 도중 아래와 같은 Logcat 에러사항이 발생했다.

 

InitConnectActivity has leaked ServiceConnection com.gastron.gfin.gfinder.InitConnectActivity$2@55400ce that was originally bound here

 

해당내용을 구글링 해본 결과 블루투스 서비스를 바인드 한 뒤 바인드를 해제하지 않은 상태에서 다시 바인드 할 때

 

이와같은 문제가 발생한다고 한다.

 

즉 onCreate 에서 서비스 바인드

 

Intent gattServiceIntent = new Intent(InitConnectActivity.this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);

 

이후에 Activity가 종료될때 unbind를 해주지 않아서 생기는 문제라는 것이다.

 

그런대 onDestroy() 함수에 

 

unregisterReceiver(mGattUpdateReceiver);
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
mBluetoothLeService = null;

 

와같이 해제를 해주었음에도 위와같은 에러는 계속 발생해서 더 찾아보니..

 

서비스를 register로 등록한게 아니라 bindService함수를 통해서 등록했기 때문에

 

서비스를 닫아줄 때에도 unbind 함수를 사용해야 에러가 나지 않는다는 것이다!!

 

unbindService(mServiceConnection);

 

즉 위와같은 한줄을 추가 해 줌으로써 서비스를 등록하고 해제하는 깔끔한 프로그램을 만들 수 있는 것이다.

댓글