안녕하세요. 두 폰간 블루투스 페어링을 해주려고 하는데 잘안되서 염치불구하고 여쭈어 봅니다.

쉽게 블루투스 페어링 감을 익히고자 버튼은 3개를 만들었습니다. 첫번째 버튼은 다른 기기들이 자신을 찾을 수 있도록 해주고, 

두번째는 다른 기기들을 검색해주는 버튼. 세번째는 페어링을 해주도록 한 버튼입니다.

하기 소스를 보시면 쉽게 아실 것 입니다. 다른분들은 소켓열고 커넥트해서 잘되시는 거 같은데 저는 앱이 죽지도 않고

설정 -> 블루투스가서 확인해보면 페어링이 되어 있지 않네요..

2가지 질문이 있는데 첫번째는 우선 A기기가 Discoverable을 AcceptThread를 실행해주고 B기기가 Discovery하여 A기기를 찾고 

페어링해줘야 하나요? 페어링을 하기 위해서는 이 과정을 항상 거쳐야 하는지요? 

아니면 찾은 기억이 있다면 Discoverable, Discovery과정없이 페어링이 바로 가능한가요?

또 두번째는 페어링만 하기 위해서 listenUsingRfcommWithServiceRecord 도 선언해줘야 되나요? 

다른 분들께서 언급을 안해주신 건지 모르겠지만 

mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);

mBtAdapter.cancelDiscovery();

mmSocket.connect();  이런식으로만 구현하셔서 되었다고 하시는데 서버쪽도 구현을 해줘야 페어링이 되는건지 궁금하네요..

하기 소스에서는 서버쪽도 구현을 해주었습니다.. 근데 안되네요..

작은 답글이라도 저에겐 큰 힘이 됩니다. 정말 부탁드립니다. 


 package my.com.test;
import java.io.IOException;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TEST extends Activity {
    
	BluetoothAdapter mBtAdapter;
	TextView MyMac,RemoteMac,Etc;
	Button Discoverable,Discovery,Pairing;
    private static String MacAddress = "00:0F:E4:A6:47:12";
    private static final UUID MY_UUID =
            UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    String NAME = "BluetoothSecure";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        String temp = mBtAdapter.getAddress();
        
        //TextView 
        MyMac = (TextView)findViewById(R.id.MyMac);
        RemoteMac = (TextView)findViewById(R.id.RemoteMac);
        Etc = (TextView)findViewById(R.id.Etc);
        
        MyMac.setText(temp);
        
        //Button 
        Discoverable = (Button)findViewById(R.id.btn1);
        Discovery = (Button)findViewById(R.id.btn2);
        Pairing = (Button)findViewById(R.id.btn3);
        
        if(!mBtAdapter.isEnabled()) {
			/*
			Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		    enableBtIntent.putExtra("RECEIVER", RECEIVER);
			startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
			*/
			mBtAdapter.enable(); //Bluetooth Turn on 
        }
        
        Discoverable.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mBtAdapter.getScanMode() !=
			            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
			            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 60);
			            startActivity(discoverableIntent);
			    } else {
			    	Toast.makeText(getApplicationContext(), "이미 실행중입니다.", Toast.LENGTH_SHORT).show();
			    }
				
				AcceptThread mAcceptThread = new AcceptThread();
				mAcceptThread.start();
			}
        });
        
        Discovery.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mBtAdapter.startDiscovery();
			}
		});
        
        Pairing.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				BluetoothDevice device = mBtAdapter.getRemoteDevice(MacAddress);
				ConnectThread mConnectThread = new ConnectThread(device);
				mConnectThread.start();
			}
       });
     
    }
    
    class ConnectThread extends Thread {
    	private final BluetoothSocket mmSocket;
    	private final BluetoothDevice mmDevice;
    	public ConnectThread(BluetoothDevice device) {
    	// Use a temporary object that is later assigned to mmSocket,
    	// because mmSocket is final
    	BluetoothSocket tmp = null;
    	mmDevice = 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) { }
    	mmSocket = tmp;
    	}
    	public void run() {
    	// Cancel discovery because it will slow down the connection
    	mBtAdapter.cancelDiscovery();
    	try {
    	// Connect the device through the socket. This will block
    	// until it succeeds or throws an exception
    	mmSocket.connect();
    	} catch (IOException connectException) {
    	// Unable to connect; close the socket and get out
    	try {
    	mmSocket.close();
    	} catch (IOException closeException) { }
    	return;
    	}
    	// Do work to manage the connection (in a separate thread)
//    	manageConnectedSocket(mmSocket);
    	}
    	/** Will cancel an in-progress connection, and close the socket */
    	public void cancel() {
    	try {
    	mmSocket.close();
    	} catch (IOException e) { }
    	}
    	}
    class AcceptThread extends Thread {
    	private final BluetoothServerSocket mmServerSocket;
    	public AcceptThread() {
    	// Use a temporary object that is later assigned to mmServerSocket,
    	// because mmServerSocket is final
    	BluetoothServerSocket tmp = null;
    	try {
    	// MY_UUID is the app's UUID string, also used by the client code
    	tmp = mBtAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
    	} catch (IOException e) { }
    	mmServerSocket = tmp;
    	}
    	public void run() {
    	BluetoothSocket socket = null;
    	// Keep listening until exception occurs or a socket is returned
    	while (true) {
    	try {
    	socket = mmServerSocket.accept();
    	} catch (IOException e) {
    	break;
    	}
    	// If a connection was accepted
    	if (socket != null) {
    	// Do work to manage the connection (in a separate thread)
//    	manageConnectedSocket(socket);
    	try{
    		mmServerSocket.close();
    	} catch(IOException e){	}
    	break;
    	}
    	}
    	}
    	/** Will cancel the listening socket, and cause the thread to finish */
    	public void cancel() {
    	try {
    	mmServerSocket.close();
    	} catch (IOException e) { }
    	}
    	}
    
}