Advertisement
Guest User

MyApp_activity

a guest
Jan 20th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.79 KB | None | 0 0
  1.  
  2. package com.example.enricocirignaco.myapplication;
  3.  
  4.         import android.app.ActionBar;
  5.         import android.app.ProgressDialog;
  6.         import android.bluetooth.BluetoothAdapter;
  7.         import android.bluetooth.BluetoothDevice;
  8.         import android.bluetooth.BluetoothSocket;
  9.         import android.content.ClipData;
  10.         import android.content.Intent;
  11.         import android.content.IntentFilter;
  12.         import android.os.AsyncTask;
  13.         import android.os.Handler;
  14.         import android.support.v7.app.AppCompatActivity;
  15.         import android.os.Bundle;
  16.         import android.util.Log;
  17.         import android.view.Menu;
  18.         import android.view.MenuItem;
  19.         import android.view.View;
  20.         import android.widget.EditText;
  21.         import android.widget.ListView;
  22.         import android.widget.TextView;
  23.         import android.widget.Toast;
  24.  
  25.         import java.io.IOException;
  26.         import java.io.InputStream;
  27.         import java.io.OutputStream;
  28.         import java.util.UUID;
  29.  
  30.  
  31. public class MainActivity extends AppCompatActivity {
  32.  
  33.     // Declaring variables
  34.     double temperature = 17.5;
  35.     double humidity = 64.3;
  36.     int pressure = 1019;
  37.     double battery = 97.70;
  38.  
  39.     double last_humidity = 23.5;
  40.     double last_temperature = 63.5;
  41.     int last_pressure = 1019;
  42.     double last_battery = 97.5;
  43.  
  44.     int battery_voltage = 3400;
  45.  
  46.     String string_temperature = String.valueOf(temperature);
  47.     String string_humidity = String.valueOf(humidity);
  48.     String string_pressure = String.valueOf(pressure);
  49.     String string_battery = String.valueOf(battery);
  50.  
  51.     String string_last_temperature = String.valueOf(last_humidity);
  52.     String string_last_humidity = String.valueOf(last_temperature);
  53.     String string_last_pressure = String.valueOf(last_pressure);
  54.     String string_last_battery = String.valueOf(last_battery);
  55.  
  56.     String string_battery_voltage = String.valueOf(battery_voltage);
  57.  
  58.     // Declaring Widget
  59.     TextView temperature_text;
  60.     TextView humidity_text;
  61.     TextView pressure_text;
  62.     TextView battery_text;
  63.  
  64.     TextView last_temperature_text;
  65.     TextView last_humidity_text;
  66.     TextView last_pressure_text;
  67.     TextView last_battery_text;
  68.  
  69.     TextView last_battery_voltage_text;
  70.  
  71.     private ProgressDialog progress;
  72.     //String address = "98:D3:32:10:84:60";       //HC_05 MAC address
  73.     String address = "AB:60:86:56:34:02";         //SPP MAC address
  74.  
  75.  
  76.     static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  77.     private boolean isBtConnected = false;
  78.  
  79.     // Declaring Bluetooth
  80.     BluetoothAdapter myBluetooth = null;
  81.     BluetoothSocket btSocket = null;
  82.  
  83.     //==============================================================================================
  84.  
  85.  
  86.     @Override
  87.     protected void onCreate(Bundle savedInstanceState) {
  88.         super.onCreate(savedInstanceState);
  89.  
  90.         // Set the layout
  91.         setContentView(R.layout.activity_main);
  92.         SetLayout();
  93.  
  94.         // Setup Bluetooth
  95.         SetupBT();
  96.  
  97.     }
  98.  
  99.  
  100.  
  101.  
  102.     //==============================================================================================
  103.  
  104.     @Override
  105.     public boolean onOptionsItemSelected(MenuItem item) {
  106.  
  107.         int id = item.getItemId();
  108.  
  109.         // if "Connect" Button has pushed
  110.         if(id == R.id.action_connect) {
  111.  
  112.             //Call the class to connect
  113.             new ConnectBT().execute();
  114.             }
  115.  
  116.         // if "disconnect" Button has pushed
  117.         if(id == R.id.action_disconnect) {
  118.  
  119.             //Call the class to disconnect
  120.             DisconnectBT();
  121.         }
  122.         if(id == R.id.action_setting_2) {
  123.             sendBTdata("a");
  124.         }
  125.         return true;
  126.     }
  127.  
  128.     //==============================================================================================
  129.  
  130.     @Override
  131.     public boolean onPrepareOptionsMenu(Menu menu) {
  132.  
  133.         invalidateOptionsMenu();
  134.         if(isBtConnected == true) {
  135.             getMenuInflater().inflate(R.menu.menu_activity_2, menu);
  136.         }
  137.         else{
  138.             getMenuInflater().inflate(R.menu.menu_activity, menu);
  139.  
  140.         }
  141.         return true;
  142.     }
  143.  
  144.     //==============================================================================================
  145.  
  146.     // Calling the widget and set text
  147.     public void SetLayout() {
  148.  
  149.         temperature_text = (TextView) findViewById(R.id.temperature_value_id);
  150.         humidity_text    = (TextView) findViewById(R.id.humidity_value_id);
  151.         pressure_text    = (TextView) findViewById(R.id.pressure_value_id);
  152.         battery_text     = (TextView) findViewById(R.id.battery_value_id);
  153.  
  154.         last_temperature_text = (TextView) findViewById(R.id.last_temperature_id);
  155.         last_humidity_text    = (TextView) findViewById(R.id.last_humidity_id);
  156.         last_pressure_text    = (TextView) findViewById(R.id.last_pressure_id);
  157.         last_battery_text     = (TextView) findViewById(R.id.last_battery_id);
  158.  
  159.         last_battery_voltage_text  = (TextView) findViewById(R.id.battery_voltage_id);
  160.  
  161.         temperature_text.setText(string_temperature + "°C");
  162.         humidity_text.setText(string_humidity + "%");
  163.         pressure_text.setText(string_pressure + "hPa");
  164.         battery_text.setText(string_battery + "%");
  165.  
  166.         last_temperature_text.setText(string_last_temperature + "°C" +"(16:23" + ")");
  167.         last_humidity_text.setText(string_last_humidity + "% (16:23" + ")");
  168.         last_pressure_text.setText(string_last_pressure + "hPa"+ "(16:23" + ")");
  169.         last_battery_text.setText(string_last_battery + "%" + "(16:23" + ")");
  170.  
  171.         last_battery_voltage_text.setText(string_battery_voltage + "mV");
  172.     }
  173.  
  174.     //==============================================================================================
  175.  
  176.     // Setup Bluetooth Options
  177.     public void SetupBT() {
  178.  
  179.         //if the device has bluetooth
  180.         myBluetooth = BluetoothAdapter.getDefaultAdapter();
  181.  
  182.         if(myBluetooth == null)
  183.         {
  184.             //Show a mensag. that the device has no bluetooth adapter
  185.             msg("Bluetooth Device Not Available");
  186.  
  187.             //finish apk
  188.             finish();
  189.         }
  190.         else if(!myBluetooth.isEnabled())
  191.         {
  192.             //Ask to the user turn the bluetooth on
  193.             Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  194.             startActivityForResult(turnBTon,1);
  195.         }
  196.  
  197.  
  198.     }
  199.  
  200.     //==============================================================================================
  201.  
  202.     // function to display a string like a Toast
  203.     private void msg(String s) {
  204.         Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
  205.     }
  206.  
  207.     //==============================================================================================
  208.  
  209.     // Connect Bluetooth Process
  210.     private class ConnectBT extends AsyncTask<Void, Void, Void> {
  211.  
  212.         private boolean ConnectSuccess = true; //if it's here, it's almost connected
  213.  
  214.         @Override
  215.         protected void onPreExecute()
  216.         {
  217.             progress = ProgressDialog.show(MainActivity.this, "Connecting...", "Please wait!!!");  //show a progress dialog
  218.         }
  219.  
  220.         @Override
  221.         protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background
  222.         {
  223.             try
  224.             {
  225.                 if (btSocket == null || !isBtConnected)
  226.                 {
  227.                     myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
  228.                     BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available
  229.                     btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
  230.                     BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
  231.                     btSocket.connect();//start connection
  232.                 }
  233.             }
  234.             catch (IOException e)
  235.             {
  236.                 ConnectSuccess = false;//if the try failed, you can check the exception here
  237.             }
  238.             return null;
  239.         }
  240.         @Override
  241.         protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
  242.         {
  243.             super.onPostExecute(result);
  244.  
  245.             if (!ConnectSuccess)
  246.             {
  247.                 msg("Connection Failed. Try again.");
  248.                 finish();
  249.             }
  250.             else
  251.             {
  252.                 msg("Connected.");
  253.                 isBtConnected = true;
  254.             }
  255.             progress.dismiss();
  256.         }
  257.     }
  258.  
  259.     //==============================================================================================
  260.  
  261.     // Disconnect Bluetooth process
  262.     private void DisconnectBT() {
  263.  
  264.         if (btSocket!=null) //If the btSocket is busy
  265.         {
  266.             try
  267.             {
  268.                 btSocket.close(); //close connection
  269.             }
  270.             catch (IOException e)
  271.             { msg("Error");}
  272.         }
  273.         msg("Disconnected");
  274.         isBtConnected = false;
  275.  
  276.     }
  277.  
  278.     //==============================================================================================
  279.  
  280.     // function to send string via bluetooth
  281.     private void sendBTdata(String d) {
  282.         if (btSocket!=null)
  283.         {
  284.             try
  285.             {
  286.                 btSocket.getOutputStream().write(d.getBytes());
  287.             }
  288.             catch (IOException e)
  289.             {
  290.                 msg("Error");
  291.             }
  292.         }
  293.  
  294.     }
  295.  
  296.     //==============================================================================================
  297.  
  298.  
  299.  
  300.  
  301. }
  302.  
  303.  
  304.  
  305.  
  306. /** #12.34*13.44*34.60*1110.78*4574   **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement