제가 하려고 하는게 휴대폰과 블루투스 모듈을 연결하는 것입니다.
버튼을 누르면 해당 블루투스모듈과 자동으로 페어링이 되게 하려고 하는데
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;
}
}