Skip to content

Instantly share code, notes, and snippets.

@elktros
Created August 3, 2019 07:41
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 elktros/62918d10b4776c2dd5d6f18e4110137f to your computer and use it in GitHub Desktop.
Save elktros/62918d10b4776c2dd5d6f18e4110137f to your computer and use it in GitHub Desktop.
Quadruped_robot_bluetoothActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class bluetoothActivity extends AppCompatActivity {
Button btnPaired;
ListView devicelist;
private BluetoothAdapter myBluetooth = null;
private Set<BluetoothDevice> pairedDevices;
public static String EXTRA_ADDRESS = "device_address";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
btnPaired = (Button)findViewById(R.id.button);
devicelist = (ListView)findViewById(R.id.listView);
myBluetooth = BluetoothAdapter.getDefaultAdapter();
//checking whether device has bluetooth
if(myBluetooth == null)
{
Toast.makeText(getApplicationContext(), "Bluetooth not Available", Toast.LENGTH_LONG).show();
finish();
}
// Bluetooth not enabled in device
else if(!myBluetooth.isEnabled())
{
//Ask the user to turn on bluetooth
Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnBTon,1);
}
btnPaired.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
pairedDevicesList();
}
});
}
// function to display paired device list
private void pairedDevicesList()
{
pairedDevices = myBluetooth.getBondedDevices();
//list to store multiple names
ArrayList list = new ArrayList();
if (pairedDevices.size()>0)
{
for(BluetoothDevice bt : pairedDevices)
{
list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address
}
}
else
{
Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show();
}
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
devicelist.setAdapter(adapter);
devicelist.setOnItemClickListener(myListClickListener); //Method called when the device from the list is clicked
}
private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener()
{
public void onItemClick (AdapterView<?> av, View v, int arg2, long arg3)
{
// Get the device MAC address, the last 17 chars in the View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// Make an intent to start next activity.
Intent i = new Intent(bluetoothActivity.this, Main2chatActivity.class);
//Change the activity.
i.putExtra(EXTRA_ADDRESS, address); //this will be received at Main2chatActivity Activity(Bluetooth address is passed)
startActivity(i);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment