Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.enricocirignaco.myapplication;
- import android.app.ActionBar;
- import android.app.ProgressDialog;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.ClipData;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.AsyncTask;
- import android.os.Handler;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.UUID;
- public class MainActivity extends AppCompatActivity {
- // Declaring variables
- double temperature = 17.5;
- double humidity = 64.3;
- int pressure = 1019;
- double battery = 97.70;
- double last_humidity = 23.5;
- double last_temperature = 63.5;
- int last_pressure = 1019;
- double last_battery = 97.5;
- int battery_voltage = 3400;
- String string_temperature = String.valueOf(temperature);
- String string_humidity = String.valueOf(humidity);
- String string_pressure = String.valueOf(pressure);
- String string_battery = String.valueOf(battery);
- String string_last_temperature = String.valueOf(last_humidity);
- String string_last_humidity = String.valueOf(last_temperature);
- String string_last_pressure = String.valueOf(last_pressure);
- String string_last_battery = String.valueOf(last_battery);
- String string_battery_voltage = String.valueOf(battery_voltage);
- // Declaring Widget
- TextView temperature_text;
- TextView humidity_text;
- TextView pressure_text;
- TextView battery_text;
- TextView last_temperature_text;
- TextView last_humidity_text;
- TextView last_pressure_text;
- TextView last_battery_text;
- TextView last_battery_voltage_text;
- private ProgressDialog progress;
- //String address = "98:D3:32:10:84:60"; //HC_05 MAC address
- String address = "AB:60:86:56:34:02"; //SPP MAC address
- static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- private boolean isBtConnected = false;
- // Declaring Bluetooth
- BluetoothAdapter myBluetooth = null;
- BluetoothSocket btSocket = null;
- //==============================================================================================
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // Set the layout
- setContentView(R.layout.activity_main);
- SetLayout();
- // Setup Bluetooth
- SetupBT();
- }
- //==============================================================================================
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- // if "Connect" Button has pushed
- if(id == R.id.action_connect) {
- //Call the class to connect
- new ConnectBT().execute();
- }
- // if "disconnect" Button has pushed
- if(id == R.id.action_disconnect) {
- //Call the class to disconnect
- DisconnectBT();
- }
- if(id == R.id.action_setting_2) {
- sendBTdata("a");
- }
- return true;
- }
- //==============================================================================================
- @Override
- public boolean onPrepareOptionsMenu(Menu menu) {
- invalidateOptionsMenu();
- if(isBtConnected == true) {
- getMenuInflater().inflate(R.menu.menu_activity_2, menu);
- }
- else{
- getMenuInflater().inflate(R.menu.menu_activity, menu);
- }
- return true;
- }
- //==============================================================================================
- // Calling the widget and set text
- public void SetLayout() {
- temperature_text = (TextView) findViewById(R.id.temperature_value_id);
- humidity_text = (TextView) findViewById(R.id.humidity_value_id);
- pressure_text = (TextView) findViewById(R.id.pressure_value_id);
- battery_text = (TextView) findViewById(R.id.battery_value_id);
- last_temperature_text = (TextView) findViewById(R.id.last_temperature_id);
- last_humidity_text = (TextView) findViewById(R.id.last_humidity_id);
- last_pressure_text = (TextView) findViewById(R.id.last_pressure_id);
- last_battery_text = (TextView) findViewById(R.id.last_battery_id);
- last_battery_voltage_text = (TextView) findViewById(R.id.battery_voltage_id);
- temperature_text.setText(string_temperature + "°C");
- humidity_text.setText(string_humidity + "%");
- pressure_text.setText(string_pressure + "hPa");
- battery_text.setText(string_battery + "%");
- last_temperature_text.setText(string_last_temperature + "°C" +"(16:23" + ")");
- last_humidity_text.setText(string_last_humidity + "% (16:23" + ")");
- last_pressure_text.setText(string_last_pressure + "hPa"+ "(16:23" + ")");
- last_battery_text.setText(string_last_battery + "%" + "(16:23" + ")");
- last_battery_voltage_text.setText(string_battery_voltage + "mV");
- }
- //==============================================================================================
- // Setup Bluetooth Options
- public void SetupBT() {
- //if the device has bluetooth
- myBluetooth = BluetoothAdapter.getDefaultAdapter();
- if(myBluetooth == null)
- {
- //Show a mensag. that the device has no bluetooth adapter
- msg("Bluetooth Device Not Available");
- //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);
- }
- }
- //==============================================================================================
- // function to display a string like a Toast
- private void msg(String s) {
- Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
- }
- //==============================================================================================
- // Connect Bluetooth Process
- private class ConnectBT extends AsyncTask<Void, Void, Void> {
- private boolean ConnectSuccess = true; //if it's here, it's almost connected
- @Override
- protected void onPreExecute()
- {
- progress = ProgressDialog.show(MainActivity.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. Try again.");
- finish();
- }
- else
- {
- msg("Connected.");
- isBtConnected = true;
- }
- progress.dismiss();
- }
- }
- //==============================================================================================
- // Disconnect Bluetooth process
- private void DisconnectBT() {
- if (btSocket!=null) //If the btSocket is busy
- {
- try
- {
- btSocket.close(); //close connection
- }
- catch (IOException e)
- { msg("Error");}
- }
- msg("Disconnected");
- isBtConnected = false;
- }
- //==============================================================================================
- // function to send string via bluetooth
- private void sendBTdata(String d) {
- if (btSocket!=null)
- {
- try
- {
- btSocket.getOutputStream().write(d.getBytes());
- }
- catch (IOException e)
- {
- msg("Error");
- }
- }
- }
- //==============================================================================================
- }
- /** #12.34*13.44*34.60*1110.78*4574 **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement