Skip to content

Instantly share code, notes, and snippets.

private void doDiscovery() {
Log.d("TAG", "doDiscovery()");
// Indicate scanning in the title
setProgressBarIndeterminateVisibility(true);
setTitle("discovering new devices");
// Turn on sub-title for new devices
findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
// Get the BluetoothDevice object from the Intent BluetoothDevice
private AdapterView.OnItemClickListener mDeviceClickListener= new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3)
{
// Cancel discovery because it's costly and we're about to connect
bluetoothAdapter.cancelDiscovery();
Toast.makeText(getApplicationContext(),"Stopping discovery",Toast.LENGTH_SHORT).show();
// Get the device MAC address, which is the last 17 chars in the View String
info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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;
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");onButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
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();
}
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
}
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;