public
class
MainActivity
extends
AppCompatActivity {
static
final
int
REQUEST_ENABLE_BT =
10
;
int
mPariedDeviceCount =
0
;
Set<BluetoothDevice> mDevices;
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mRemoteDevie;
BluetoothSocket mSocket =
null
;
OutputStream mOutputStream =
null
;
InputStream mInputStream =
null
;
Thread mWorkerThread =
null
;
ImageButton Cuser;
ImageButton admin;
BluetoothSPP bt;
TextView textView;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBluetooth();
bt =
new
BluetoothSPP(
this
);
textView = findViewById(R.id.textView);
Cuser = findViewById(R.id.userbutton);
admin = findViewById(R.id.adminbutton);
bt.setOnDataReceivedListener(
new
BluetoothSPP.OnDataReceivedListener() {
public
void
onDataReceived(
byte
[] data, String message) {
textView.setText(message);
}
});
Cuser.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Intent commonUser =
new
Intent(getApplicationContext(), com.example.autotapingmachine.CommonUser.
class
);
startActivity(commonUser);
}
});
admin.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Intent adminuser =
new
Intent(getApplicationContext(),Administrator.
class
);
startActivity(adminuser);
}
});
}
BluetoothDevice getDeviceFromBondedList(String name) {
BluetoothDevice selectedDevice =
null
;
for
(BluetoothDevice deivce : mDevices) {
if
(name.equals(deivce.getName())) {
selectedDevice = deivce;
break
;
}
}
return
selectedDevice;
}
void
connectToSelectedDevice(String selectedDeviceName) {
mRemoteDevie = getDeviceFromBondedList(selectedDeviceName);
UUID uuid = java.util.UUID.fromString(
"00001101-0000-1000-8000-00805f9b34fb"
);
try
{
mSocket = mRemoteDevie.createInsecureRfcommSocketToServiceRecord(uuid);
mSocket.connect();
}
catch
(Exception e) {
Toast.makeText(getApplicationContext(),
"블루투스 연결 중 오류가 발생했습니다."
, Toast.LENGTH_LONG).show();
finish();
}
}
void
selectDevice() {
mDevices = mBluetoothAdapter.getBondedDevices();
mPariedDeviceCount = mDevices.size();
if
(mPariedDeviceCount ==
0
) {
Toast.makeText(getApplicationContext(),
"페어링된 장치가 없습니다."
, Toast.LENGTH_LONG).show();
finish();
}
AlertDialog.Builder builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"블루투스 장치 선택"
);
List<String> listItems =
new
ArrayList<String>();
for
(BluetoothDevice device : mDevices) {
listItems.add(device.getName());
}
listItems.add(
"취소"
);
final
CharSequence[] items = listItems.toArray(
new
CharSequence[listItems.size()]);
listItems.toArray(
new
CharSequence[listItems.size()]);
builder.setItems(items,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
item) {
if
(item == mPariedDeviceCount) {
Toast.makeText(getApplicationContext(),
"연결할 장치를 선택하지 않았습니다."
, Toast.LENGTH_LONG).show();
finish();
}
else
{
connectToSelectedDevice(items[item].toString());
}
}
});
builder.setCancelable(
false
);
AlertDialog alert = builder.create();
alert.show();
}
void
checkBluetooth() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if
(mBluetoothAdapter ==
null
) {
Toast.makeText(getApplicationContext(),
"기기가 블루투스를 지원하지 않습니다."
, Toast.LENGTH_LONG).show();
finish();
}
else
{
if
(!mBluetoothAdapter.isEnabled()) {
Toast.makeText(getApplicationContext(),
"현재 블루투스가 비활성 상태입니다."
, Toast.LENGTH_LONG).show();
Intent enableBtIntent =
new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else
selectDevice();
}
}
@Override
protected
void
onDestroy() {
try
{
mWorkerThread.interrupt();
mInputStream.close();
mSocket.close();
}
catch
(Exception e){}
super
.onDestroy();
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
if
(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if
(resultCode == Activity.RESULT_OK)
bt.connect(data);
}
switch
(requestCode) {
case
REQUEST_ENABLE_BT:
if
(resultCode == RESULT_OK) {
selectDevice();
}
else
if
(resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"블루투스를 사용할 수 없어 프로그램을 종료합니다"
, Toast.LENGTH_LONG).show();
finish();
}
break
;
}
super
.onActivityResult(requestCode, resultCode, data);
}
public
void
onStart() {
super
.onStart();
if
(!bt.isBluetoothEnabled()) {
Intent intent =
new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
}
else
{
if
(!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
}
}
}
}