This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |