package
com.example.ledonoff;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.io.UnsupportedEncodingException;
import
java.util.UUID;
import
android.app.Activity;
import
android.bluetooth.BluetoothAdapter;
import
android.bluetooth.BluetoothDevice;
import
android.bluetooth.BluetoothSocket;
import
android.content.Intent;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.Toast;
public
class
LEDOnOff
extends
Activity {
private
static
final
String TAG =
"LEDOnOff"
;
Button btnMove1, btnMove2;
private
static
final
int
REQUEST_ENABLE_BT =
1
;
private
BluetoothAdapter btAdapter =
null
;
private
BluetoothSocket btSocket =
null
;
private
OutputStream outStream =
null
;
private
static
final
UUID MY_UUID =
UUID.fromString(
"00001101-0000-1000-8000-00805F9B34FB"
);
private
static
String address =
"20:13:02:21:00:73"
;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
Log.d(TAG,
"In onCreate()"
);
setContentView(R.layout.activity_main);
btnMove1 = (Button) findViewById(R.id.move1);
btnMove2 = (Button) findViewById(R.id.move2);
btAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();
btnMove1.setOnClickListener(
new
OnClickListener() {
public
void
onClick(View v) {
sendData(
"A"
);
}
});
btnMove2.setOnClickListener(
new
OnClickListener() {
public
void
onClick(View v) {
sendData(
"B"
);
}
});
}
public
static
String stringToHex(String str){
String result =
""
;
for
(
int
i=
0
; i<str.length(); i++){
result += String.format(
"%02X"
, (
int
)str.charAt(i));
}
return
result;
}
@Override
public
void
onResume() {
super
.onResume();
Log.d(TAG,
"...In onResume - Attempting client connect..."
);
BluetoothDevice device = btAdapter.getRemoteDevice(address);
try
{
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
}
catch
(IOException e) {
errorExit(
"Fatal Error"
,
"In onResume() and socket create failed: "
+ e.getMessage() +
"."
);
}
btAdapter.cancelDiscovery();
Log.d(TAG,
"...Connecting to Remote..."
);
try
{
btSocket.connect();
Log.d(TAG,
"...Connection established and data link opened..."
);
}
catch
(IOException e) {
try
{
btSocket.close();
}
catch
(IOException e2) {
errorExit(
"Fatal Error"
,
"In onResume() and unable to close socket during connection failure"
+ e2.getMessage() +
"."
);
}
}
Log.d(TAG,
"...Creating Socket..."
);
try
{
outStream = btSocket.getOutputStream();
}
catch
(IOException e) {
errorExit(
"Fatal Error"
,
"In onResume() and output stream creation failed:"
+ e.getMessage() +
"."
);
}
}
@Override
public
void
onPause() {
super
.onPause();
Log.d(TAG,
"...In onPause()..."
);
if
(outStream !=
null
) {
try
{
outStream.flush();
}
catch
(IOException e) {
errorExit(
"Fatal Error"
,
"In onPause() and failed to flush output stream: "
+ e.getMessage() +
"."
);
}
}
try
{
btSocket.close();
}
catch
(IOException e2) {
errorExit(
"Fatal Error"
,
"In onPause() and failed to close socket."
+ e2.getMessage() +
"."
);
}
}
private
void
checkBTState() {
if
(btAdapter==
null
) {
errorExit(
"Fatal Error"
,
"Bluetooth Not supported. Aborting."
);
}
else
{
if
(btAdapter.isEnabled()) {
Log.d(TAG,
"...Bluetooth is enabled..."
);
}
else
{
Intent enableBtIntent =
new
Intent(btAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
private
void
errorExit(String title, String message){
Toast msg = Toast.makeText(getBaseContext(),
title +
" - "
+ message, Toast.LENGTH_SHORT);
msg.show();
finish();
}
private
void
sendData(String message) {
byte
[] msgBuffer =
null
;
try
{
String str =
new
String(message.getBytes(),
"UTF-8"
);
msgBuffer = str.getBytes();
}
catch
(UnsupportedEncodingException e1) {
e1.printStackTrace();
}
Log.d(TAG,
"...Sending data: "
+ message +
"..."
);
try
{
outStream.write(msgBuffer);
}
catch
(IOException e) {
String msg =
"In onResume() and an exception occurred during write: "
+ e.getMessage();
if
(address.equals(
"20:13:02:21:00:73"
))
msg = msg +
".\n\nUpdate your server address from 20:13:02:21:00:73 to the correct address on line 37 in the java code"
;
msg = msg +
".\n\nCheck that the SPP UUID: "
+ MY_UUID.toString() +
" exists on server.\n\n"
;
errorExit(
"Fatal Error"
, msg);
}
}
}