public
class
MainActivity
extends
AppCompatActivity {
TextView mTvBluetoothStatus;
TextView mTvReceiveData;
TextView mTvSendData;
Button mBtnBluetoothOn;
Button mBtnBluetoothOff;
Button mBtnConnect;
Button mBtnSendData;
BluetoothAdapter mBluetoothAdapter;
Set<BluetoothDevice> mPairedDevices;
List<String> mListPairedDevices;
Handler mBluetoothHandler;
ConnectedBluetoothThread mThreadConnectedBluetooth;
BluetoothDevice mBluetoothDevice;
BluetoothSocket mBluetoothSocket;
final
static
int
BT_REQUEST_ENABLE =
1
;
final
static
int
BT_MESSAGE_READ =
2
;
final
static
int
BT_CONNECTING_STATUS =
3
;
final
static
UUID BT_UUID = UUID.fromString(
"00001101-0000-1000-8000-00805F9B34FB"
);
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvBluetoothStatus = (TextView)findViewById(R.id.tvBluetoothStatus);
mTvReceiveData = (TextView)findViewById(R.id.tvReceiveData);
mTvSendData = (EditText) findViewById(R.id.tvSendData);
mBtnBluetoothOn = (Button)findViewById(R.id.btnBluetoothOn);
mBtnBluetoothOff = (Button)findViewById(R.id.btnBluetoothOff);
mBtnConnect = (Button)findViewById(R.id.btnConnect);
mBtnSendData = (Button)findViewById(R.id.btnSendData);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBtnBluetoothOn.setOnClickListener(
new
Button.OnClickListener() {
@Override
public
void
onClick(View view) {
bluetoothOn();
}
});
mBtnBluetoothOff.setOnClickListener(
new
Button.OnClickListener() {
@Override
public
void
onClick(View view) {
bluetoothOff();
}
});
mBtnConnect.setOnClickListener(
new
Button.OnClickListener() {
@Override
public
void
onClick(View view) {
listPairedDevices();
}
});
mBtnSendData.setOnClickListener(
new
Button.OnClickListener() {
@Override
public
void
onClick(View view) {
if
(mThreadConnectedBluetooth !=
null
) {
mThreadConnectedBluetooth.write(mTvSendData.getText().toString());
mTvSendData.setText(
""
);
}
}
});
mBluetoothHandler =
new
Handler(){
public
void
handleMessage(android.os.Message msg){
if
(msg.what == BT_MESSAGE_READ){
String readMessage =
null
;
try
{
readMessage =
new
String((
byte
[]) msg.obj,
"UTF-8"
);
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
mTvReceiveData.setText(readMessage);
}
}
};
}
void
bluetoothOn() {
if
(mBluetoothAdapter ==
null
) {
Toast.makeText(getApplicationContext(),
"블루투스를 지원하지 않는 기기입니다."
, Toast.LENGTH_LONG).show();
}
else
{
if
(mBluetoothAdapter.isEnabled()) {
Toast.makeText(getApplicationContext(),
"블루투스가 이미 활성화 되어 있습니다."
, Toast.LENGTH_LONG).show();
mTvBluetoothStatus.setText(
"활성화"
);
}
else
{
Toast.makeText(getApplicationContext(),
"블루투스가 활성화 되어 있지 않습니다."
, Toast.LENGTH_LONG).show();
Intent intentBluetoothEnable =
new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intentBluetoothEnable, BT_REQUEST_ENABLE);
}
}
}
void
bluetoothOff() {
if
(mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
Toast.makeText(getApplicationContext(),
"블루투스가 비활성화 되었습니다."
, Toast.LENGTH_SHORT).show();
mTvBluetoothStatus.setText(
"비활성화"
);
}
else
{
Toast.makeText(getApplicationContext(),
"블루투스가 이미 비활성화 되어 있습니다."
, Toast.LENGTH_SHORT).show();
}
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
switch
(requestCode) {
case
BT_REQUEST_ENABLE:
if
(resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(),
"블루투스 활성화"
, Toast.LENGTH_LONG).show();
mTvBluetoothStatus.setText(
"활성화"
);
}
else
if
(resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"취소"
, Toast.LENGTH_LONG).show();
mTvBluetoothStatus.setText(
"비활성화"
);
}
break
;
}
super
.onActivityResult(requestCode, resultCode, data);
}
void
listPairedDevices() {
if
(mBluetoothAdapter.isEnabled()) {
mPairedDevices = mBluetoothAdapter.getBondedDevices();
if
(mPairedDevices.size() >
0
) {
AlertDialog.Builder builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"장치 선택"
);
mListPairedDevices =
new
ArrayList<String>();
for
(BluetoothDevice device : mPairedDevices) {
mListPairedDevices.add(device.getName());
}
final
CharSequence[] items = mListPairedDevices.toArray(
new
CharSequence[mListPairedDevices.size()]);
mListPairedDevices.toArray(
new
CharSequence[mListPairedDevices.size()]);
builder.setItems(items,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
item) {
connectSelectedDevice(items[item].toString());
}
});
AlertDialog alert = builder.create();
alert.show();
}
else
{
Toast.makeText(getApplicationContext(),
"페어링된 장치가 없습니다."
, Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(),
"블루투스가 비활성화 되어 있습니다."
, Toast.LENGTH_SHORT).show();
}
}
void
connectSelectedDevice(String selectedDeviceName) {
for
(BluetoothDevice tempDevice : mPairedDevices) {
if
(selectedDeviceName.equals(tempDevice.getName())) {
mBluetoothDevice = tempDevice;
break
;
}
}
try
{
mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(BT_UUID);
mBluetoothSocket.connect();
mThreadConnectedBluetooth =
new
ConnectedBluetoothThread(mBluetoothSocket);
mThreadConnectedBluetooth.start();
mBluetoothHandler.obtainMessage(BT_CONNECTING_STATUS,
1
, -
1
).sendToTarget();
}
catch
(IOException e) {
Toast.makeText(getApplicationContext(),
"블루투스 연결 중 오류가 발생했습니다."
, Toast.LENGTH_LONG).show();
}
}