public classTerminalActivityextendsActivity {
BluetoothSPPbt;
TextViewtextStatus,textRead;
EditTextetMessage;
Menumenu;
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_terminal);
Log.i("Check","onCreate");
textRead= (TextView)findViewById(R.id.textRead);
textStatus= (TextView)findViewById(R.id.textStatus);
etMessage= (EditText)findViewById(R.id.etMessage);
bt=newBluetoothSPP(this);
if(!bt.isBluetoothAvailable()) {
Toast.makeText(getApplicationContext()
,"Bluetooth is not available"
, Toast.LENGTH_SHORT).show();
finish();
}
bt.setOnDataReceivedListener(newOnDataReceivedListener() {
public voidonDataReceived(byte[] data, String message) {
textRead.append(message +"\n");
}
});
bt.setBluetoothConnectionListener(newBluetoothConnectionListener() {
public voidonDeviceDisconnected() {
textStatus.setText("Status : Not connect");
menu.clear();
getMenuInflater().inflate(R.menu.menu_connection,menu);
}
public voidonDeviceConnectionFailed() {
textStatus.setText("Status : Connection failed");
}
public voidonDeviceConnected(String name, String address) {
textStatus.setText("Status : Connected to "+ name);
menu.clear();
getMenuInflater().inflate(R.menu.menu_disconnection,menu);
}
});
}
public booleanonCreateOptionsMenu(Menu menu) {
this.menu= menu;
getMenuInflater().inflate(R.menu.menu_connection, menu);
return true;
}
public booleanonOptionsItemSelected(MenuItem item) {
intid = item.getItemId();
if(id == R.id.menu_android_connect) {
bt.setDeviceTarget(BluetoothState.DEVICE_ANDROID);
/*
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED)
bt.disconnect();*/
Intent intent =newIntent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}else if(id == R.id.menu_device_connect) {
bt.setDeviceTarget(BluetoothState.DEVICE_OTHER);
/*
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED)
bt.disconnect();*/
Intent intent =newIntent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}else if(id == R.id.menu_disconnect) {
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED)
bt.disconnect();
}
return super.onOptionsItemSelected(item);
}
public voidonDestroy() {
super.onDestroy();
bt.stopService();
}
public voidonStart() {
super.onStart();
if(!bt.isBluetoothEnabled()) {
Intent intent =newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
}else{
if(!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_ANDROID);
setup();
}
}
}
public voidsetup() {
Button btnSend = (Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener(newOnClickListener(){
public voidonClick(View v){
if(etMessage.getText().length() !=0) {
bt.send(etMessage.getText().toString(),true);
etMessage.setText("");
}
}
});
}
public voidonActivityResult(intrequestCode,intresultCode, Intent data) {
if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if(resultCode == Activity.RESULT_OK)
bt.connect(data);
}else if(requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if(resultCode == Activity.RESULT_OK) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_ANDROID);
setup();
}else{
Toast.makeText(getApplicationContext()
,"Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
finish();
}
}
}
}