Facebook
From Chunky Goose, 6 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 265
  1. public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
  2.  
  3.     private final static UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  4.     protected VerticalSeekBar kierunek;
  5.     protected VerticalSeekBar predkosc;
  6.     protected TextView wyswietlPredkosc;
  7.     protected TextView wyswietlKierunek;
  8.     protected TextView wartoscNapiecia;
  9.     protected int wartoscKierunku=0;
  10.     protected int wartoscPredkosci=0;
  11.     protected BluetoothAdapter mojBluetooth;
  12.     protected ToggleButton polacz;
  13.     protected ToggleButton wysylaj;
  14.     protected ToggleButton spoczynekButton;
  15.     protected ToggleButton wsteczny;
  16.     protected ToggleButton odbieraj;
  17.     private static final int ENABLE_BT_REQUEST_CODE = 1;
  18.     protected int wysylanyKierunek=0;//przechowuje zmienne dotyczące zmiany kierunku jazdy
  19.     protected int wysylanaPredkosc=0;//przechowuje zmienne dotyczące zmiany prędkości
  20.     protected int wstecznyBoolean=0;//ustala czy samochód ma jechać w przód czy w tył
  21.     protected int R1=2120;//rezystancja opornika 1
  22.     protected int R2=4550;//rezystancja opornika 2
  23.     private BluetoothSocket bluetoothSocket;
  24.     private BluetoothDevice bluetoothDevice;
  25.     private OutputStream outputstream;//obiekt umożliwiający wysyłanie danych
  26.     private InputStream inputstream;//obiekt umożliwiający odbiór danych
  27.     protected Handler mHandler;//obiekt odpowiedzialny za przechwyt odpowiednich danych z wątku
  28.     private int SPOCZYNEK_STAN=0;//zmienna stanu
  29.     private int ODBIOR_STAN=1;//zmienna stanu
  30.     private int WYSYLAJ_STAN=0;//zmienna stanu
  31.     private int CZAS_STAN=0;//zmienna stanu
  32.     private int ODBIOR_WATEK_STAN=0;//zmienna stanu
  33.  
  34.  
  35.  
  36.     @Override
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.activity_main);
  40.         kierunek=(VerticalSeekBar)findViewById(R.id.KIERUNEK);
  41.         predkosc=(VerticalSeekBar)findViewById(R.id.PREDKOSC);
  42.         wyswietlPredkosc=(TextView)findViewById(R.id.wyswietl_predkosc);
  43.         wyswietlKierunek=(TextView)findViewById(R.id.wyswietl_kierunek);
  44.         polacz=(ToggleButton)findViewById(R.id.POLACZ);
  45.         wysylaj=(ToggleButton)findViewById(R.id.WYSYLAJ);
  46.         spoczynekButton=(ToggleButton)findViewById(R.id.SPOCZYNEK);
  47.         wsteczny=(ToggleButton)findViewById(R.id.WSTECZNY);
  48.         odbieraj= (ToggleButton) findViewById(R.id.ODBIERAJ);
  49.         wartoscNapiecia= (TextView) findViewById(R.id.WARTOSC_NAPIECIA);
  50.         final Timer czas;
  51.         czas=new Timer();
  52.         final MyTimerTask myTimerTask;
  53.         myTimerTask = new MyTimerTask();
  54.  
  55.         wlaczanieBluetooth();
  56.  
  57.         polacz.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  58.             @Override
  59.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  60.                 if (b) {
  61.                     BluetoothDevice bluetoothDevice = mojBluetooth.getRemoteDevice("98:D3:33:80:66:65");
  62.                     ConnectingThread t = new ConnectingThread(bluetoothDevice);
  63.                     t.start();
  64.                     while(bluetoothSocket.isConnected()==false)
  65.                     {
  66.                     }
  67.                     Toast toast2 = Toast.makeText(getApplicationContext(), "Połączono", Toast.LENGTH_LONG);
  68.                     toast2.show();
  69.                     //SprawdzeniePolaczenia sprawdzeniePolaczenia=new SprawdzeniePolaczenia();
  70.                     //sprawdzeniePolaczenia.start();
  71.                 } else {
  72.                     if(bluetoothSocket.isConnected()==true) {
  73.                         try {
  74.                             bluetoothSocket.close();
  75.                         } catch (IOException e) {
  76.                             e.printStackTrace();
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.         });
  82.         wysylaj.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  83.             @Override
  84.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  85.                 if (b)
  86.                 {
  87.                     if(bluetoothSocket.isConnected()==true) {
  88.                         ODBIOR_STAN = 0;
  89.                         WYSYLAJ_STAN = 1;
  90.                         odbieraj.setChecked(false);
  91.                         if (CZAS_STAN == 0) {
  92.                             czas.schedule(myTimerTask, 0, 50);
  93.                             CZAS_STAN = 1;
  94.                         }
  95.                         if (ODBIOR_STAN == 0) {
  96.                             wartoscNapiecia.setText("Wyłącz wysyłanie, aby wyświetlić napięcie");
  97.                             Toast toast = Toast.makeText(getApplicationContext(), "Wyłącz wysyłanie, aby odebrać napięcie", Toast.LENGTH_SHORT);
  98.                             toast.show();
  99.                         }
  100.                     }
  101.                     else
  102.                     {
  103.                         Toast toast = Toast.makeText(getApplicationContext(), "Brak połączenia", Toast.LENGTH_SHORT);
  104.                         toast.show();
  105.                         wysylaj.setChecked(false);
  106.                     }
  107.                 }
  108.                 else
  109.                 {
  110.                     WYSYLAJ_STAN=0;
  111.                 }
  112.             }
  113.         });
  114.         spoczynekButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  115.             @Override
  116.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  117.                 if(b)
  118.                 {
  119.                     predkosc.setProgressAndThumb(0);
  120.                     kierunek.setProgressAndThumb(kierunek.getMax()/2);
  121.                     SPOCZYNEK_STAN=1;
  122.                 }
  123.                 else
  124.                 {
  125.                     SPOCZYNEK_STAN=0;
  126.                 }
  127.             }
  128.         });
  129.         wsteczny.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  130.             @Override
  131.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  132.                 if(b)
  133.                 {
  134.                     wstecznyBoolean=50;
  135.                     predkosc.setProgressAndThumb(0);
  136.                 }
  137.                 else
  138.                 {
  139.                     wstecznyBoolean=0;
  140.                     predkosc.setProgressAndThumb(0);
  141.                 }
  142.             }
  143.         });
  144.         odbieraj.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  145.             @Override
  146.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  147.                 if (b){
  148.                     if(bluetoothSocket.isConnected()==true)
  149.                     {
  150.                         if(WYSYLAJ_STAN==0)
  151.                         {
  152.                             ODBIOR_STAN=1;
  153.                         }
  154.                         else
  155.                         {
  156.                             odbieraj.setChecked(false);
  157.                             Toast toast = Toast.makeText(getApplicationContext(), "Wyłącz wysyłanie, aby odebrać napięcie", Toast.LENGTH_SHORT);
  158.                             toast.show();
  159.                         }
  160.                         if(ODBIOR_WATEK_STAN==0){
  161.                             ODBIOR_WATEK_STAN=1;
  162.                             WatekOdbierania watekOdbierania=new WatekOdbierania();
  163.                             watekOdbierania.start();
  164.                         }
  165.                     }
  166.                     else
  167.                     {
  168.                         Toast toast = Toast.makeText(getApplicationContext(), "Brak połączenia", Toast.LENGTH_SHORT);
  169.                         toast.show();
  170.                         odbieraj.setChecked(false);
  171.                     }
  172.                     }
  173.  
  174.                 else
  175.                 {
  176.                     ODBIOR_STAN=0;
  177.                 }
  178.             }
  179.         });
  180.         mHandler=new Handler(){
  181.             @Override
  182.             public void handleMessage(Message msg) {
  183.                 wyswietlOdebraneDane(msg, R1, R2);
  184.             }
  185.         };
  186.         zmianaKierunku();
  187.         zmianaPredkosci();
  188.     }
  189.  
  190.  
  191.  
  192.     private void wlaczanieBluetooth() {
  193.         mojBluetooth=BluetoothAdapter.getDefaultAdapter();
  194.         if(mojBluetooth==null)
  195.         {
  196.             Toast.makeText(getApplicationContext(),"Brak modułu bluetooth!",Toast.LENGTH_LONG).show();
  197.         }
  198.         else
  199.         {
  200.             if(!mojBluetooth.isEnabled())
  201.             {
  202.                 Intent intent=new Intent(mojBluetooth.ACTION_REQUEST_ENABLE);
  203.                 startActivityForResult(intent, ENABLE_BT_REQUEST_CODE);
  204.             }
  205.         }
  206.     }
  207.  
  208.  
  209.  
  210.     protected void zmianaKierunku()
  211.     {
  212.         kierunek.setOnSeekBarChangeListener(
  213.                 new VerticalSeekBar.OnSeekBarChangeListener() {
  214.                     @Override
  215.                     public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  216.                         wartoscKierunku = 2 * (kierunek.getProgress() - 45);
  217.                         wyswietlKierunek.setText("" + wartoscKierunku);
  218.                     }
  219.  
  220.                     @Override
  221.                     public void onStartTrackingTouch(SeekBar seekBar) {
  222.  
  223.                     }
  224.  
  225.                     @Override
  226.                     public void onStopTrackingTouch(SeekBar seekBar) {
  227.  
  228.                     }
  229.                 }
  230.  
  231.         );
  232.     }
  233.  
  234.     protected void zmianaPredkosci() {
  235.         predkosc.setOnSeekBarChangeListener(
  236.                 new VerticalSeekBar.OnSeekBarChangeListener() {
  237.                     @Override
  238.                     public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  239.                         wartoscPredkosci = 2 * (predkosc.getProgress() - 50);
  240.                         wyswietlPredkosc.setText("" + wartoscPredkosci);
  241.                     }
  242.  
  243.                     @Override
  244.                     public void onStartTrackingTouch(SeekBar seekBar) {
  245.  
  246.  
  247.                     }
  248.  
  249.                     @Override
  250.                     public void onStopTrackingTouch(SeekBar seekBar) {
  251.  
  252.  
  253.                     }
  254.                 }
  255.         );
  256.     };
  257.  
  258.     private class ConnectingThread extends Thread {
  259.  
  260.         public ConnectingThread(BluetoothDevice device) {
  261.  
  262.             BluetoothSocket temp = null;
  263.             bluetoothDevice = device;
  264.  
  265.             // Get a BluetoothSocket to connect with the given BluetoothDevice
  266.             try {
  267.                 temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
  268.             } catch (IOException e) {
  269.                 e.printStackTrace();
  270.             }
  271.             bluetoothSocket = temp;
  272.         }
  273.  
  274.         public void run() {
  275.             try {
  276.                 bluetoothSocket.connect();
  277.                 outputstream=bluetoothSocket.getOutputStream();
  278.                 inputstream=bluetoothSocket.getInputStream();
  279.             } catch (IOException connectException) {
  280.                 connectException.printStackTrace();
  281.                 try {
  282.                     bluetoothSocket.close();
  283.                 } catch (IOException closeException) {
  284.                     closeException.printStackTrace();
  285.                 }
  286.             }
  287.             }
  288.         public void cancel() {
  289.             try {
  290.                 bluetoothSocket.close();
  291.             } catch (IOException e) {
  292.                 e.printStackTrace();
  293.             }
  294.         }
  295.     }
  296.  
  297.     protected void wyslij(int s) throws IOException {
  298.         outputstream.write(s);
  299.     }