안드로이드 개발 질문/답변
(글 수 45,052)
Bluetooth 관련 통신을 하고 있는데 소켓 생성 부분에서 IOException (Bad file number) 가 발생하고 있습니다.
그런데 해결 방법을 잘 모르겠어서 문의 드립니다.
// 소켓 생성 부분
private void connectBluetooth() throws IOException
{
if(mServerSocket == null)
{
mServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("BluetoothChat", MY_UUID); // Exception 발생하는 부분
mBluetoothAdapter.getState();
}
mBluetoothSocket = mServerSocket.accept();
mInputStream = mBluetoothSocket.getInputStream();
mOutputStream = mBluetoothSocket.getOutputStream();
}// 연결 종료하는 부분
private void disconnectBluetooth() throws IOException
{
if(mInputStream != null)
{
mInputStream.close();
mInputStream = null;
}
if(mOutputStream != null)
{
mOutputStream.close();
mOutputStream = null;
}
if(mBluetoothSocket != null)
{
mBluetoothSocket.close();
mBluetoothSocket = null;
}
if (mServerSocket != null)
{
mServerSocket.close();
mServerSocket = null;
}
}
이게 처음 연결 시작할때부터 발생하는 에러는 아니고,
연결 했다 끊었다를 몇차례 반복하면 발생합니다.
10-22 20:52:43.110: E/SPP(4771): ++ connect() error = Bad file number
에러 발생시 로그는 위와 같은데, 에러 로그가 찍히기 직전까지는 정상 동작시와 동일한 로그를 출력하고 있습니다.
혹시 RFCOMM 관련 소켓 갯수에 제한이 있어서 그 소켓이 제대로 close 되지 않아 갯수 초과해서 발생하는 문제인가요?
그렇다면 close() 메쏘드 호출 말고 확실히 종료 시킬 수 있는 방법이 있는지 궁금합니다.
안드로이드 버전 2.3 대에서 자주 발생하는 것 같고, ICS 이상 버전 (갤럭시S3) 에서는 현상이 나타나지 않네요.
답변 부탁 드립니다.
감사합니다.



