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

블루투스 연결 IOException 부분 에러

0 추천
제가 하려고 하는게 휴대폰과 블루투스 모듈을 연결하는 것입니다.

버튼을 누르면 해당 블루투스모듈과 자동으로 페어링이 되게 하려고 하는데

 IOException 부분에서 에러가 있는지 TAG, "nono"에 걸립니다. ㅠㅠ

어떻게 해결 할 수 있을까요?? 블루투스 모듈은 FB155BC(HID) 입니다.

 

package com.example.all;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import android.view.View.OnClickListener;
import android.view.View;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.*;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.app.Activity;
import android.view.Menu;
import  android.util.Log;

public class MainActivity extends Activity {
 
 private  static  final  String TAG = "BluetoothChat";
 private  static  final  boolean  D = true;
 public static final UUID MY_UUID = UUID.fromString("00000003-0000-1000-8000-00805F9B34FB"); //create space
 private static final int REQUEST_ENABLE_BT = 3;
 private BluetoothAdapter mBluetoothAdapter = null;
 private String mMacaddress="00:19:01:37:6B:37";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // 내 기기의 블루투스 가능 여부
        mBluetoothAdapter  =  BluetoothAdapter.getDefaultAdapter();

       
        Button connectBtn = (Button)findViewById(R.id.connectBtn);
        connectBtn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:19:01:37:6B:37");
    ConnectThread connect = new ConnectThread(device);
    connect.start();
    
   }
  });
       
      
       
        if(mBluetoothAdapter == null) {
         Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
         finish();
         return ;
         
        }
 }
 
 private class ConnectThread extends Thread {
     private BluetoothSocket BTSocket;
     private final BluetoothDevice mDevice;

    
     public ConnectThread(BluetoothDevice device) {
         // Use a temporary object that is later assigned to mmSocket,
         // because mmSocket is final
      if(D) Log.d(TAG, "connect tread ");
         BluetoothSocket tmp = null;
         mDevice = device;
         // Get a BluetoothSocket to connect with the given BluetoothDevice
         try {
             // MY_UUID is the app's UUID string, also used by the server code
             tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
         } catch (IOException e) { }
         BTSocket = tmp;
     }

     public void run() { //run connectthread
        

         try {
             // Connect the device through the socket. This will block
             // until it succeeds or throws an exception
             BTSocket.connect();        
         } catch (IOException connectException) {
          
          
             // Unable to connect; close the socket and get out
             try {
              if(D) Log.d(TAG, "nono");
                BTSocket.close();
             } catch (IOException closeException) { }
             return;
         }

         // Do work to manage the connection (in a separate thread)
             //manageConnectedSocket(BTSocket);
     }

     /*public void cancel() {
         try {
             BTSocket.close();
         } catch (IOException e) { }
     } */
 }

 
   
 
 public void onStart() {
  super.onStart();
  // 블루투스 활성화 요청
  if(!mBluetoothAdapter.isEnabled()) {
   Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
   startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
   
  }
 }
  
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(D) Log.d(TAG, "onActivityResult " + resultCode);
  switch(requestCode) {
  
  case REQUEST_ENABLE_BT:
   if(resultCode == Activity.RESULT_OK) {
    Toast.makeText(this, "Bluetooth is Connected", Toast.LENGTH_LONG).show();
    
   }
   
  else {
    Toast.makeText(this, "Bluetooth is not Connected", Toast.LENGTH_LONG).show();
   }
  }
 }
 

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}
익명사용자 님이 2013년 10월 11일 질문

1개의 답변

0 추천
에러의 상세한 내용을 안봐서는 모르겠지만 UUID가 맞는건가요..?

모듈이 SPP / HID를 지원하는것 같은데

만약 SPP라면 00001101-0000-1000-8000-00805F9B34FB같은데요..

전혀 동떨어진 답변이었다면 죄송합니다~~ ㅠ.ㅠ
Seraph (1,520 포인트) 님이 2013년 10월 11일 답변
Seraph님이 2013년 10월 11일 수정
UUID를 바꿔봤는데도 원하는 동작이 안되네요 ㅠ
UUID의 문제는 아닌 것 같습니다.
...