Skip to content

Instantly share code, notes, and snippets.

@sidishere
Created December 24, 2019 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidishere/94101e8b62e51ee4564c139d451d7bf7 to your computer and use it in GitHub Desktop.
Save sidishere/94101e8b62e51ee4564c139d451d7bf7 to your computer and use it in GitHub Desktop.
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.util.UUID;
public class LedControl extends AppCompatActivity {
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
Button onButton, offButton, disconnectButton;
private ProgressDialog progress;
String address=null;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private boolean isBtConnected = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_led_control);
onButton = (Button) findViewById(R.id.onButton);
offButton = (Button) findViewById(R.id.offButton);
disconnectButton = (Button) findViewById(R.id.disconnectButton);
Intent newint = getIntent();
address = newint.getStringExtra("DEVICE_ADDRESS");//receive the address of the bluetooth device
Log.i("APP",address);
new ConnectBT().execute();
onButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
turnOnLed();      //method to turn on
}
});
offButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
turnOffLed();   //method to turn off
}
});
disconnectButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Disconnect(); //close connection
}
});
}
//Helper methods
private void Disconnect()
{
if (btSocket!=null) //If the btSocket is busy
{
try
{
btSocket.close(); //close connection
Toast.makeText(getApplicationContext(),"Connection closed successfully",Toast.LENGTH_SHORT).show();
}
catch (IOException e)
{
Toast.makeText(getApplicationContext(),"Error in closing connection",Toast.LENGTH_SHORT).show();
}
}
finish(); //return to the first layout
}
private void turnOffLed()
{
if (btSocket!=null)
{
try
{
btSocket.getOutputStream().write("TF".toString().getBytes());
}
catch (IOException e)
{
Toast.makeText(getApplicationContext(),"Error in sending command",Toast.LENGTH_SHORT).show();
}
}
}
private void turnOnLed()
{
if (btSocket!=null)
{
try
{
btSocket.getOutputStream().write("TO".toString().getBytes());
}
catch (IOException e)
{
Toast.makeText(getApplicationContext(),"Error in sending command",Toast.LENGTH_SHORT).show();
}
}
}
//Async task class to establishb luetooth connection
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(LedControl.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
Log.i("APP","Trying "+btSocket.toString());
btSocket.connect();//start connection
Log.i("APP","After Trying "+btSocket.toString());
}
}
catch (IOException e)
{
ConnectSuccess = false;//if the try failed, you can check the exception here
Log.i("APP","Connection failed "+e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
{
super.onPostExecute(result);
if (!ConnectSuccess)
{
Toast.makeText(getApplicationContext(),"Connection Failed. Is it a SPP Bluetooth? Try again.",Toast.LENGTH_SHORT).show();
Log.i("APP","Conn status"+ConnectSuccess);
finish();
}
else
{
Toast.makeText(getApplicationContext(),"Connected.",Toast.LENGTH_SHORT).show();
isBtConnected = true;
}
progress.dismiss();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment