안녕하세요
안드로이드 블루투스 프로그래밍 입문한지 이제 일주일 된 학생입니다.
다름이 아니라 블루투스 연결하는 것 까지 되었는데요
A, B, C 의 블루투스 기계가 있다고 가정한다면,
A의 기계의 연결만을 허용하고 싶습니다.
이부분이 해결이 되지 않아 글을 남깁니다.
다음 소스 코드에서 어떤 부분을 수정해야 하는지 좀 알려주세요
A기계에서 휴대폰의 맥주소를 가지고 연결시도를 하게 되고
스마트 폰에서는 연결 시도를 받게 되면, 연결되는 방식으로 코딩 되었습니다.
A 기계에서 스마트 폰으로 연결 시도 할때, A의 맥주소를 볼순 없을까요?
다음은 제가 작성한 소스코드 입니다
package kr.co.TestBluetooth;
import java.io.IOException;
import java.util.Set;
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 TestBluetoothActivity extends Activity {
/** Called when the activity is first created. */
// Message types sent from the BluetoothConnection Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
// Intent request codes
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;
BluetoothAdapter mBtAdapter;
TextView MyMac,RemoteMac,Etc;
Button Discoverable,Discovery,Pairing;
private static String MacAddress;
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();
MyMac = (TextView)findViewById(R.id.MyMac);
RemoteMac = (TextView)findViewById(R.id.RemoteMac);
Etc = (TextView)findViewById(R.id.Etc);
MyMac.setText(temp);
Discoverable = (Button)findViewById(R.id.btn1);
Discovery = (Button)findViewById(R.id.btn2);
Pairing = (Button)findViewById(R.id.btn3);
if(!mBtAdapter.isEnabled()) {
mBtAdapter.enable(); //Bluetooth Turn on
}
// mBtAdapter.getScanMode();
AcceptThread mAcceptThread = new AcceptThread();
mAcceptThread.start();
try{
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
if(pairedDevices.size() != 0){
for(BluetoothDevice device : pairedDevices){
Toast.makeText(this, device.getName() + "\n" + device.getAddress(), Toast.LENGTH_LONG).show();
//mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
// if(device.getName().equals("iPhone")){
// MacAddress = device.getAddress();
// String addr = MacAddress.substring(MacAddress.length() - 17);
// Toast.makeText(this, "addr : "+addr.toString(), Toast.LENGTH_LONG).show();
// Intent intent = new Intent();
// intent.putExtra(NAME, addr);
// setResult(Activity.RESULT_OK, intent);
// }
}
}
}catch(Exception ex){
Toast.makeText(this, "pairedDevice "+ex.toString(), Toast.LENGTH_LONG).show();
}
Discoverable.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
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() {
public void onClick(View v) {
// TODO Auto-generated method stub
mBtAdapter.startDiscovery();
}
});
Pairing.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
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) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
}catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
mBtAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
}
try {
mmSocket.close();
} catch(IOException closeException){
}
return;
}
public void cancel(){
try{
mmSocket.close();
}catch(IOException e){}
}
}
class AcceptThread extends Thread{
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
try {
tmp = mBtAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) { }
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
while (true) {
try {
socket = mmServerSocket.accept();
}catch (IOException e) {
break;
}
if (socket != null) {
try{
mmServerSocket.close();
}catch(IOException e){ }
break;
}
}
}
public void cancel() {
try {
mmServerSocket.close();
}catch (IOException e) { }
}
}
}
여기에서 어느부분을 수정하고, 어떤 코드를 추가 해야 하는지 좀 알려주세요
꼭 좀 부탁 드립니다. (__) 이제 곧 블루투스 연결부분이 마무리 될것 같은데 꼭 좀 부탁 드립니다.