LAMPIRAN A : LISTING PROGRAM JAVA ANDROID
MainActivity.java
package com.TA.bluetooth;
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Set;
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.os.Handler; import android.os.Message; import android.util.Log;
import android.view.MotionEvent; import android.view.View;
import android.view.View.OnTouchListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView;
public class MainActivity extends Activity implements View.OnTouchListener{
// Debugging
private static final String TAG = "BlueComm"; private static final boolean D = true;
// deklarasi
private BluetoothAdapter mBluetoothAdapter; private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice; private InputStream mmInputStream; private OutputStream mmOutputStream; private Button ButtonOpen;
private Button ButtonClose; private ImageButton ButtonMove; private ImageButton ButtonBack; private ImageButton ButtonRight; private ImageButton ButtonLeft; private TextView Label;
private TextView stat; private TextView act; byte[] Buffer;
int readBufferPosition; int counter;
volatile boolean stopWorker; public String dta;
public ArrayAdapter mArrayAdapter; public String strParam="menunggu";
// Untuk menerima message dari background thread Handler h = new Handler()
{
@Override
public void handleMessage (Message m) {
super.handleMessage(m);
if(D) Log.d(TAG, "Handle Message"); Bundle bund =m.getData();
String key = bund.getString("My Key"); }
};
// On Create Activity
@Override
public void onCreate(Bundle savedInstanceState) {
if(D) Log.d(TAG, "On Create"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
ButtonOpen = (Button)findViewById(R.id.ButtonOpen); ButtonClose = (Button)findViewById(R.id.ButtonClose); ButtonMove = (ImageButton)findViewById(R.id.ButtonTop); ButtonBack = (ImageButton)findViewById(R.id.ButtonBelow); ButtonRight = (ImageButton)findViewById(R.id.ButtonRight); ButtonLeft = (ImageButton)findViewById(R.id.ButtonLeft); //TextOutput = (TextView)findViewById(R.id.TextOutput); Label = (TextView)findViewById(R.id.label);
stat = (TextView)findViewById(R.id.textViewStatus); act= (TextView)findViewById(R.id.textViewAction);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter == null)
{
Label.setText("No bluetooth adapter available"); }
if(!mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0); }
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) { // TODO Auto-generated method stub
act.setText(String.valueOf(arg1.getAction())); if(arg0==ButtonMove) strParam="m";
if(arg1.getAction()!=1){ try{
sendData(strParam); }
catch (IOException ex) { } }
return false; }
// On Start Activity
@Override
public void onStart() {
super.onStart();
if(D) Log.e(TAG, "ON START"); //Open Button
ButtonOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
CariBT(); BukaBT(); }
catch (IOException ex) { } }
});
//Close button
ButtonClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
closeBT(); }
catch (IOException ex) { } }
});
ButtonMove.setOnTouchListener(this); ButtonBack.setOnTouchListener(this); ButtonLeft.setOnTouchListener(this); ButtonRight.setOnTouchListener(this);
}
// Mencari Client Bluetooth yg sudah paired dengan nama “nutscientist“
void CariBT() {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0) {
for(BluetoothDevice device : pairedDevices) {
if(device.getName().equals("HC-05")) {
mmDevice = device;
Label.setText("Slave Client Found"); //break;
} else {
Label.setText("Slave Client Not Found"); }
@Override
protected void onStop() { super.onStop();
if(D) Log.e(TAG, "ON STOP"); }
@Override
protected void onDestroy() { super.onDestroy();
if(D) Log.e(TAG, "ON DESTROY"); }
@Override
protected void onResume() {
super.onResume();
if(D) Log.e(TAG, "+ ON RESUME +"); }
@Override
protected void onPause() { super.onPause();
if(D) Log.e(TAG, "- ON PAUSE -"); }
// Untuk memulai/ melakukan koneksi dengan client bluetooth void BukaBT() throws IOException
{
if(D) Log.d(TAG, "Buka BT");
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); ListenForData();
Label.setText("Bluetooth Opened");
//Toast.makeText(this, "Bluetooth Dibuka", Toast.LENGTH_LONG).show();
// Background thread untuk menerima data dari client bluetooth void ListenForData()
{
if(D) Log.d(TAG, "Listen Data");
final byte delimiter = 13; //atau Carriage Return (CR), di gunakan untuk mendeteksi akhir dari text
stopWorker = false; readBufferPosition = 0; Buffer = new byte[1024];
Thread workerThread = new Thread(new Runnable() {
@Override
public void run() {
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
//Do work try {
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(Buffer, 0, encodedBytes, 0, encodedBytes.length); final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
Message m = new Message();
Bundle bund = new Bundle();
bund.putString("My Key", data);
m.setData(bund);
h.sendMessage(m);
} else {
} }
} }
catch (IOException ex) {
stopWorker = true; }
} }
});
workerThread.start(); }
// Untuk mengirim data ke client bluetooth void sendData(String m) throws IOException {
if(D) Log.d(TAG, "Kirim Data");
mmOutputStream.write(m.getBytes()); Label.setText("Data Sent");
stat.setText(m);
//Toast.makeText(this, “Data Terkirim“, Toast.LENGTH_LONG).show();
}
// Untuk menutup/mengakhiri koneksi dengan client bluetooth void closeBT() throws IOException
{
if(D) Log.d(TAG, "Tutup BT"); stopWorker = true;
mmOutputStream.close(); mmInputStream.close(); mmSocket.close();
Label.setText("Bluetooth Closed");
//Toast.makeText(this, “Bluetooth Ditutup“,
Toast.LENGTH_LONG).show(); }
Activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/OpenButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > >
<ImageButton
android:id="@+id/ButtonTop"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:background="@drawable/arrow_top"
android:contentDescription="@drawable/arrow_top" />
<ImageButton
android:id="@+id/ButtonLeft"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/ButtonTop"
android:layout_toLeftOf="@+id/ButtonTop"
android:background="@drawable/arrow_left"
android:contentDescription="@drawable/arrow_left" />
<ImageButton
android:id="@+id/ButtonRight"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/ButtonTop"
android:layout_toRightOf="@+id/ButtonTop"
android:background="@drawable/arrow_right"
android:contentDescription="@drawable/arrow_right" />
<ImageButton
android:id="@+id/ButtonBelow"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/ButtonRight"
android:layout_toLeftOf="@+id/ButtonRight"
android:background="@drawable/arrow_below" />
<Button
android:id="@+id/ButtonOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ButtonLeft"
android:layout_below="@+id/ButtonBelow"
android:layout_marginTop="35dp"
android:text="@string/Open"
<Button
android:id="@+id/ButtonClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ButtonOpen"
android:layout_alignBottom="@+id/ButtonOpen"
android:layout_alignRight="@+id/ButtonRight"
android:text="@string/Close"
android:textColor="@color/close" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ButtonClose"
android:layout_centerHorizontal="true"
android:layout_marginBottom="7dp"
android:text="@string/app_name" />
<TextView
android:id="@+id/textViewStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ButtonOpen"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="TextView" />
<TextView
android:id="@+id/textViewAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewStatus"
android:layout_below="@+id/textViewStatus"
android:layout_marginTop="8dp"
android:text="TextView" />
#include <mega32.h> #include <stdio.h> #include <delay.h> unsigned char der; void maju(){
PORTD.2=0; PORTD.3=1;
PORTD.7=1; PORTD.6=0; OCR1A=100; OCR1B=100; }
// Declare your global variables here
void b_kiri(){
PORTD.2=1; PORTD.3=0;
PORTD.7=1; PORTD.6=0; OCR1A=100; OCR1B=100; }
void b_kanan(){
PORTD.2=0; PORTD.3=1;
PORTD.7=0; PORTD.6=1; OCR1A=100; OCR1B=100;
}
void mundur(){ PORTD.2=1; PORTD.3=0;
PORTD.7=0; PORTD.6=1; OCR1A=100; OCR1B=100; }
void stop(){ PORTD.2=0; PORTD.3=0;
int i=0;
void main(void) {
// Declare your local variables here
// Input/Output Ports initialization // Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=0 State0=0
PORTB=0x00; DDRB=0xFE;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00; DDRD=0xFE;
// Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 11,719 kHz // Mode: Fast PWM top=0x00FF // OC1A output: Non-Inv. // OC1B output: Non-Inv. // Noise Canceler: Off
// Input Capture on Falling Edge // Timer1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0xA1;
TCCR1B=0x0D; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00;
// Clock value: Timer2 Stopped // Mode: Normal top=0xFF
// OC2 output: Disconnected ASSR=0x00;
TCCR2=0x00; TCNT2=0x00; OCR2=0x00;
// External Interrupt(s) initialization // INT0: Off
// INT1: Off MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On
// USART Transmitter: On // USART Mode: Asynchronous // USART Baud Rate: 9600 UCSRA=0x00;
UCSRB=0x18; UCSRC=0x06; UBRRH=0x00; UBRRL=0x4D;
// Analog Comparator initialization // Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80;
SFIOR=0x00;
// ADC initialization // ADC disabled
ADCSRA=0x00;
// SPI initialization // SPI disabled
SPCR=0x00;
// TWI initialization // TWI disabled
TWCR=0x00;
while (1) {
// Place your code here
der = getchar (); // der berfungsi menampung nilai dari bT
if ( der == 'M' || der == 'm'){ maju();
mundur(); delay_ms(100);
// printf("mundur \n");
}
else if(der == 'R' || der == 'r'){ b_kanan();
delay_ms(100);
// printf("right \n"); }
else if(der == 'L' || der == 'l'){ b_kiri();
delay_ms(100);
// printf("left \n"); }
else {
delay_ms(100); // printf("%c \n",der);
}
// printf("mundur"); stop();
}