package capstone.design.project;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//import com.example.android.BluetoothChat.BluetoothChat;
//import com.example.android.BluetoothChat.BluetoothChatService;
public class Capstone_design_projActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String TAG = "BluetoothChatService";
private static final char CONTROL_LEFT = 'a';
private static final char CONTROL_FRONT = 'w';
private static final char CONTROL_BACK = 's';
private static final char CONTROL_RIGHT = 'd';
private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;
int order;
private final BluetoothAdapter mAdapter = null;
private final Handler mHandler = null;
Random r=new Random();
private BluetoothAdapter mBluetoothAdapter = null;
private ArrayAdapter<String> mArrayAdapter = null;
private BluetoothDevice mBluetoothDevice = null;
private static final UUID MY_UUID =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
EditText ET_TB2;
Button B_LEFT;
Button B_RIGHT;
Button B_FRONT;
Button B_BACK;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e("dzdz","dzdz");
final EditText tb1 = (EditText) findViewById(R.id.text1);
ET_TB2 = (EditText) findViewById(R.id.text2);
B_LEFT = (Button)findViewById(R.id.button1);
B_RIGHT = (Button)findViewById(R.id.button2);
B_FRONT = (Button)findViewById(R.id.button3);
B_BACK = (Button)findViewById(R.id.button4);
B_LEFT.setOnClickListener(this);
B_RIGHT.setOnClickListener(this);
B_FRONT.setOnClickListener(this);
B_BACK.setOnClickListener(this);
// BluetoothAdapter mBTAdapter = BluetoothAdapter.getDefaultAdapter();
// if (mBTAdapter == null) {
// tb2.setText(String.valueOf("no!!"));
// // device does not support Bluetooth
//
// return ;
// }
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if ( mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
if (!mBluetoothAdapter.isEnabled()) { //꺼져있는 블루투스를 켜보자!
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
ET_TB2.setText(String.valueOf("connected!!"));
Log.e("dddd","dddd");
}
Log.e("yiy","yiy");
Button scanButton = (Button) findViewById(R.id.button5);
scanButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
doDiscovery();
v.setVisibility(View.GONE);
ET_TB2.setText("Yes!!");
}
});
// bluetooth(getIntent());
// Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// if (pairedDevices.size() > 0) {
// for (BluetoothDevice device : pairedDevices) {
// mArrayAdapter.add(device.getName() + device.getAddress());
//// if(device.getName().equals("mcu_name")){
//// mBluetoothDevice=device;
// }
// }
// }
// mBluetoothAdapter.startDiscovery();
// // ??// Create a // BroadcastReceiverBroadcastReceiverfor ACTION_FOUNDfor FOUND
// private final BroadcastReceiverBroadcastReceivermReceivermReceiver = new BroadcastReceiver() {
// public void onReceive(Context context, Intent intent){
// String action = intent.getActionintent.getAction();
// // When discovery finds a device// device
// if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//// …………………………………………
// }
// }};
// // Register the BroadcastReceiverBroadcastReceiver
// IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
// registerReceiver(mReceiver, filter);
// }
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1:
ET_TB2.setText("LEFT");
break;
case R.id.button2:
ET_TB2.setText("RIGHT");
break;
case R.id.button3:
ET_TB2.setText("FRONT");
break;
case R.id.button4:
ET_TB2.setText("BACK");
break;
}
}
// public BluetoothChatService(Context context, Handler handler) {
// mAdapter = BluetoothAdapter.getDefaultAdapter();
// mState = STATE_NONE;
// mHandler = handler;
// }
public void doDiscovery(){
if(mAdapter.isDiscovering()){
mAdapter.cancelDiscovery();
}
mAdapter.startDiscovery();
}
}
디바이스 기기검색 부분이 실행이 되질 않네요 ㅠㅠ
버튼 중에서 x버튼을 누르면 기기 검색이 실행되야 하는데
실행이 되지 않고 갑자기 오류를 띄우며 종료됩니다...ㅠㅠ
어떻게 해야 되나요 ㅠㅠ




BluetoothAdapter 변수를 mAdapter 와 mBluetoothAdapter 두개 선언해 놓으시고, mAdapter은 사용 안하시는거 같은데,
디스커버리 함수(doDiscovery)에서는 mAdapter 를 이용하셔서 널포인트에러 떨어지는 것 같습니다.
mAdapter변수는 지우시고, 디스커버리 함수(doDiscovery)에서 mAdapter 대신 mBluetoothAdapter를 사용하시면 될 것 같습니다.