Koding Remot Kontrol ( Mobil )

MainActivity.java
------------------------
package com.example.tomi.robotautomation;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends AppCompatActivity
{
    Context context = this;

    //widgets
    ImageButton btnPaired,btnback;
    ListView devicelist;
    //Bluetooth
    private BluetoothAdapter myBluetooth = null;
    private Set<BluetoothDevice> pairedDevices;
    public static String EXTRA_ADDRESS = "device_address";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getSupportActionBar().setTitle("Koneksi Bluetooth");
        ActionBar menu = getSupportActionBar();
        menu.setDisplayShowHomeEnabled(true);
        menu.setDisplayHomeAsUpEnabled(true);

        //Calling widgets
        btnPaired = (ImageButton) findViewById(R.id.imageButton2);

        devicelist = (ListView)findViewById(R.id.Listview);

        btnback = (ImageButton) findViewById(R.id.imageButton);
        btnback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Konfirmasi..");
                builder.setMessage("Yakin Anda Ingin Menutup Aplikasi ?");
                // Membuat tombol negativ
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                //Membuat tombol positif
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Bila pilih ok, maka muncul toast
                        Toast.makeText(getApplicationContext(), "Anda Memilih Keluar", Toast.LENGTH_SHORT).show();
                        MainActivity.this.finish();
                    }
                });
                builder.show();
            }

        });


        //if the device has bluetooth
        myBluetooth = BluetoothAdapter.getDefaultAdapter();

        if(myBluetooth == null)
        {
            //Show a mensag. that the device has no bluetooth adapter
            Toast.makeText(getApplicationContext(), "Bluetooth Device Not Available", Toast.LENGTH_LONG).show();

            //finish apk
            finish();
        }
        else if(!myBluetooth.isEnabled())
        {
            //Ask to the user turn the bluetooth on
            Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnBTon,1);
        }

        btnPaired.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                pairedDevicesList();
            }
        });



    }

    private void pairedDevicesList()
    {
        pairedDevices = myBluetooth.getBondedDevices();
        ArrayList list = new ArrayList();

        if (pairedDevices.size()>0)
        {
            for(BluetoothDevice bt : pairedDevices)
            {
                list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address
            }
        }
        else
        {
            Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show();
        }

        final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
        devicelist.setAdapter(adapter);
        devicelist.setOnItemClickListener(myListClickListener); //Method called when the device from the list is clicked

    }

    private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener()
    {
        public void onItemClick (AdapterView<?> av, View v, int arg2, long arg3)
        {
            // Get the device MAC address, the last 17 chars in the View
            String info = ((TextView) v).getText().toString();
            String address = info.substring(info.length() - 17);

            // Make an intent to start next activity.
            Intent i = new Intent(MainActivity.this,Remot.class);

            //Change the activity.
            i.putExtra(EXTRA_ADDRESS, address); //this will be received at ledControl (class) Activity
            startActivity(i);
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_device_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }



        return super.onOptionsItemSelected(item);
    }
   
}

Remot.java
---------------
package com.example.tomi.robotautomation;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import java.io.IOException;
import java.util.UUID;

public class Remot extends AppCompatActivity {
    Context context = this;
ImageButton maju,mundur,kiri,kanan,berhenti,kembali,onoff,line;
    String address = null;
    private ProgressDialog progress;
    BluetoothAdapter myBluetooth = null;
    BluetoothSocket btSocket = null;
    private boolean isBtConnected = false;
    //SPP UUID. Look for it
    static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent newint = getIntent();
        address = newint.getStringExtra(MainActivity.EXTRA_ADDRESS); //receive the address of the bluetooth device

        setContentView(R.layout.activity_remot);
        new ConnectBT().execute(); //Call the class to connect

        ActionBar menu = getSupportActionBar();
        menu.setDisplayShowHomeEnabled(true);
        menu.setDisplayHomeAsUpEnabled(true);

        maju = (ImageButton) findViewById(R.id.imageButton4);
        maju.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_MOVE)
                {
                maju();
                }

                if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    berhenti();
                }

                return false;
            }
        });

        mundur=(ImageButton) findViewById(R.id.imageButton5);
        mundur.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    mundur();
                }
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    berhenti();
                }
                return false;
            }
        });

        kiri=(ImageButton) findViewById(R.id.imageButton6);
        kiri.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    kiri();
                }
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    berhenti();
                }
                return false;
            }
        });

        kanan=(ImageButton) findViewById(R.id.imageButton7);
        kanan.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    kanan();
                }
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    berhenti();
                }
                return false;
            }
        });

        berhenti=(ImageButton) findViewById(R.id.imageButton3);
        berhenti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                berhenti();
            }
        });

        onoff=(ImageButton) findViewById(R.id.imageButton9);
        onoff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                berhenti();
                PopupMenu popup = new PopupMenu(Remot.this,line);
                // Inflating menu using xml file
                popup.getMenuInflater().inflate(R.menu.remote, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                       if (item.getItemId() == R.id.of) {
                            stopline();
                            berhenti();
                            Disconnect();
                            Intent kem=new Intent(Remot.this,MainActivity.class);
                            Remot.this.startActivity(kem);
                            Remot.this.finish();
                        }
                        else if (item.getItemId() == R.id.stopline) {
                            stopline();
                        }
                        else if (item.getItemId() == R.id.lineall) {
                            Toast.makeText(getApplicationContext(), "Maaf...Line Untuk Semua Area Belum Saya Buat..!.Tunggu Ya !", Toast.LENGTH_LONG).show();
                        }
                        return true;
                    }
                });
                popup.show();        //-------------------
            }
        });



        line=(ImageButton)findViewById(R.id.imageButton8);
        line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopline();
                PopupMenu popup = new PopupMenu(Remot.this,line);
                // Inflating menu using xml file
                popup.getMenuInflater().inflate(R.menu.menuline, popup.getMenu());

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if (item.getItemId() == R.id.lineA) {
                         areaA();
                        }
                        else if (item.getItemId() == R.id.LineB) {
                         areaB();
                        }
                        else if (item.getItemId() == R.id.lineC) {
                        areaC();
                        }
                        else if (item.getItemId() == R.id.linesemua) {
                            Toast.makeText(getApplicationContext(), "Maaf...Line Untuk Semua Area Belum Saya Buat..!.Tunggu Ya !", Toast.LENGTH_LONG).show();
                        }
                        return true;
                    }
                });
                popup.show();        //-------------------
            }
        });

                  

        kembali=(ImageButton) findViewById(R.id.imageButton10);
        kembali.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Konfirmasi..");
                builder.setMessage("Yakin Anda Ingin Menutup Aplikasi ?");
                // Membuat tombol negativ
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                //Membuat tombol positif
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Bila pilih ok, maka muncul toast
                        Toast.makeText(getApplicationContext(), "Anda Memilih Keluar", Toast.LENGTH_SHORT).show();
                        stopline();
                        berhenti();
                        Disconnect();
                        Intent kem=new Intent(Remot.this,MainActivity.class);
                        Remot.this.startActivity(kem);
                        Remot.this.finish();
                    }
                });
                builder.show();
            }

        });

//-----------------akhir set
    }

    private void Disconnect()
    {
        if (btSocket!=null) //If the btSocket is busy
        {
            try
            {
                berhenti();
                btSocket.close(); //close connection
            }
            catch (IOException e)
            { msg("Error");}
        }
        finish(); //return to the first layout

    }

    private void mundur()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("B".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }

    private void maju()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("F".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }

    //-------------
    private void kiri()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("L".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }


    private void kanan()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("R".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }


    private void berhenti()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("T".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }

    private void stopline()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("7".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }
    //--------------------------------LINE
    private void areaA()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("6".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }
    private void areaB()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("8".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }
    private void areaC()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("9".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }
    // fast way to call Toast
    private void msg(String s)
    {
        Toast.makeText(getApplicationContext(),s, Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.remote, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class ConnectBT extends AsyncTask<Void, Void, Void>  // UI thread
    {
        private boolean ConnectSuccess = true; //if it's here, it's almost connected

        @Override
        protected void onPreExecute()
        {
            progress = ProgressDialog.show(Remot.this, "Connecting...", "Please wait!!!");  //show a progress dialog
        }

        @Override
        protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background
        {
            try
            {
                if (btSocket == null || !isBtConnected)
                {
                    myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
                    BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available
                    btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
                    BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                    btSocket.connect();//start connection
                }
            }
            catch (IOException e)
            {
                ConnectSuccess = false;//if the try failed, you can check the exception here
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
        {
            super.onPostExecute(result);

            if (!ConnectSuccess)
            {
                msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
                finish();
            }
            else
            {
                msg("Connected.");
                isBtConnected = true;
            }
            progress.dismiss();
        }
    }
   
}

menu_device_list.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.tomi.robotautomation.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
    <item
        android:id="@+id/about"
        android:title="Tentang Aplikasi"
        app:showAsAction="never"/>

    <item
        android:id="@+id/setting"
        android:title="Pengaturan"
        app:showAsAction="never" />

    <item
        android:id="@+id/help"
        android:title="Bantuan"
        app:showAsAction="never"/>

</menu>

menuremote.xml
----------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/on"
        android:title="Ganti Device Bluetoot"
        app:showAsAction="never"/>

    <item
        android:id="@+id/of"
        android:title="Matikan Dan Selesai"
        app:showAsAction="never" />

    <item
        android:id="@+id/stopline"
        android:title="Matikan Line Folower"
        app:showAsAction="never" />

    <item
        android:id="@+id/linesemua"
        android:title="Semua Area"
        app:showAsAction="never" />
</menu>


menuline.xml
------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/lineA"
        android:title="Line Area Kerja A"
        app:showAsAction="never"/>

    <item
        android:id="@+id/LineB"
        android:title="Line Area Kerja B"
        app:showAsAction="never" />

    <item
        android:id="@+id/lineC"
        android:title="Line Area Kerja C"
        app:showAsAction="never"/>

    <item
        android:id="@+id/lineall"
        android:title="Semua Line Area Kerja"
        app:showAsAction="never"/>
   
</menu>

menumain.xml
-------------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/about"
        android:title="Tentang Aplikasi"
        app:showAsAction="never"/>

    <item
        android:id="@+id/setting"
        android:title="Pengaturan"
        app:showAsAction="never" />

    <item
        android:id="@+id/help"
        android:title="Bantuan"
        app:showAsAction="never"/>
</menu>


Manifest.xml
-----------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tomi.robotautomation">
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ikon"
        android:label="Remote Control Robotic"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Pengaturan" />
        <activity android:name=".Remot"
            android:label="@string/Remot"
            android:theme="@style/SecondaryTheme" />
            />
        <activity android:name=".Bantuan"></activity>
    </application>

</manifest>

----remot.java

package com.example.tomi.robotautomation;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import java.io.IOException;
import java.util.UUID;

public class Remot extends AppCompatActivity {
    Context context = this;
ImageButton maju,mundur,kiri,kanan,berhenti,kembali,onoff,line;
    Button t;
    String address = null;
    private ProgressDialog progress;
    BluetoothAdapter myBluetooth = null;
    BluetoothSocket btSocket = null;
    private boolean isBtConnected = false;
    //SPP UUID. Look for it
    static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent newint = getIntent();
        address = newint.getStringExtra(MainActivity.EXTRA_ADDRESS); //receive the address of the bluetooth device

        setContentView(R.layout.activity_remot);
        new ConnectBT().execute(); //Call the class to connect

        ActionBar menu = getSupportActionBar();
        menu.setDisplayShowHomeEnabled(true);
        menu.setDisplayHomeAsUpEnabled(true);
        t=(Button) findViewById(R.id.bt);
        maju = (ImageButton) findViewById(R.id.imageButton4);
        maju.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_MOVE)
                {
                maju();
                    t.setText("MAJU");
                }

                if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    berhenti();
                    t.setText("");
                }

                return false;
            }
        });

        mundur=(ImageButton) findViewById(R.id.imageButton5);
        mundur.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    mundur();
                    t.setText("MUNDUR");

                }
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    berhenti();
                    t.setText("");
                }
                return false;
            }
        });

        kiri=(ImageButton) findViewById(R.id.imageButton6);
        kiri.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    kiri();
                    t.setText("BELOK KIRI");

                }
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    berhenti();

                    t.setText("");
                }
                return false;
            }
        });

        kanan=(ImageButton) findViewById(R.id.imageButton7);
        kanan.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    kanan();
                    t.setText("BELOK KANAN");

                }
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    berhenti();
                    t.setText("");
                }
                return false;
            }
        });

        berhenti=(ImageButton) findViewById(R.id.imageButton3);
        berhenti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                berhenti();
                t.setText("BERHENTI");

            }
        });

        onoff=(ImageButton) findViewById(R.id.imageButton9);
        onoff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                berhenti();
                PopupMenu popup = new PopupMenu(Remot.this,line);
                // Inflating menu using xml file
                popup.getMenuInflater().inflate(R.menu.remote, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                       if (item.getItemId() == R.id.of) {
                            stopline();
                            berhenti();
                            Disconnect();
                            Intent kem=new Intent(Remot.this,MainActivity.class);
                            Remot.this.startActivity(kem);
                            Remot.this.finish();
                        }
                        else if (item.getItemId() == R.id.stopline) {
                            stopline();
                           t.setText("BERHENTI");

                        }
                        return true;
                    }
                });
                popup.show();        //-------------------
            }
        });



        line=(ImageButton)findViewById(R.id.imageButton8);
        line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopline();
                berhenti();
                t.setText("Reset Line Robot");
                PopupMenu popup = new PopupMenu(Remot.this,line);
                // Inflating menu using xml file
                popup.getMenuInflater().inflate(R.menu.menuline, popup.getMenu());

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if (item.getItemId() == R.id.lineA) {
                         areaA();
                            t.setText("Robot Ke Line Area A");

                        }
                        else if (item.getItemId() == R.id.LineB) {
                         areaB();
                            t.setText("Robot Ke Line Area B");
                        }
                        else if (item.getItemId() == R.id.lineC) {
                        areaC();
                            t.setText("Robot Ke Line Area C");
                        }
                        else if (item.getItemId() == R.id.lineall) {
                            Toast.makeText(getApplicationContext(), "Maaf...Line Untuk Semua Area Belum Saya Buat..!.Tunggu Ya !", Toast.LENGTH_LONG).show();
                            //t.setText("Robot Ke Line Area A");
                        }
                        return true;
                    }
                });
                popup.show();        //-------------------
            }
        });

        kembali=(ImageButton) findViewById(R.id.imageButton10);
        kembali.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Konfirmasi..");
                builder.setMessage("Yakin Anda Ingin Menutup Aplikasi ?");
                // Membuat tombol negativ
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                //Membuat tombol positif
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Bila pilih ok, maka muncul toast
                        Toast.makeText(getApplicationContext(), "Anda Memilih Keluar", Toast.LENGTH_SHORT).show();
                        stopline();
                        berhenti();
                        Disconnect();
                        Intent kem=new Intent(Remot.this,MainActivity.class);
                        Remot.this.startActivity(kem);
                        Remot.this.finish();
                    }
                });
                builder.show();
            }

        });

//-----------------akhir set
    }

    private void Disconnect()
    {
        if (btSocket!=null) //If the btSocket is busy
        {
            try
            {
                berhenti();
                btSocket.close(); //close connection
            }
            catch (IOException e)
            { msg("Error");}
        }
        finish(); //return to the first layout

    }

    private void mundur()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("B".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }

    private void maju()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("F".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }

    //-------------
    private void kiri()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("L".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }


    private void kanan()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("R".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }


    private void berhenti()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("T".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }

    private void stopline()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("7".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }
    //--------------------------------LINE
    private void areaA()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("6".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }
    private void areaB()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("8".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }
    private void areaC()
    {
        if (btSocket!=null)
        {
            try
            {
                btSocket.getOutputStream().write("9".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }
    // fast way to call Toast
    private void msg(String s)
    {
        Toast.makeText(getApplicationContext(),s, Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.remote, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        else if (item.getItemId() == R.id.about) {
            startActivity(new Intent(this,Pengaturan.class));
        }
        else if (item.getItemId() == R.id.setting) {
            startActivity(new Intent(this, Pengaturan.class));
        }
        else if (item.getItemId() == R.id.help) {
            startActivity(new Intent(this, Bantuan.class));
        }


        return super.onOptionsItemSelected(item);
    }

    private class ConnectBT extends AsyncTask<Void, Void, Void>  // UI thread
    {
        private boolean ConnectSuccess = true; //if it's here, it's almost connected

        @Override
        protected void onPreExecute()
        {
            progress = ProgressDialog.show(Remot.this, "Connecting...", "Please wait!!!");  //show a progress dialog
        }

        @Override
        protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background
        {
            try
            {
                if (btSocket == null || !isBtConnected)
                {
                    myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
                    BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available
                    btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
                    BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                    btSocket.connect();//start connection
                }
            }
            catch (IOException e)
            {
                ConnectSuccess = false;//if the try failed, you can check the exception here
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
        {
            super.onPostExecute(result);

            if (!ConnectSuccess)
            {
                msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
                finish();
            }
            else
            {
                msg("Connected.");
                isBtConnected = true;
            }
            progress.dismiss();
        }
    }

}