Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.led.GUI;
- import android.content.Intent;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- 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 DeviceList extends ActionBarActivity //main class
- {
- //GUI objects
- Button btnPaired;
- ListView devicelist;
- //Bluetooth instance variables
- private BluetoothAdapter myBluetooth = null;
- private Set<BluetoothDevice> pairedDevices;
- public static String EXTRA_ADDRESS = "device_address";
- @Override
- protected void onCreate(Bundle savedInstanceState) //when the GUI instance (activity_device_list) is created
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_device_list); //set the current view to device list xml
- //initializing GUI objects, basically linking them with the xml file objects
- btnPaired = (Button)findViewById(R.id.button);
- devicelist = (ListView)findViewById(R.id.listView);
- //if the phone has bluetooth, get the adapter
- myBluetooth = BluetoothAdapter.getDefaultAdapter();
- if(myBluetooth == null) //if there is no bluetooth device on the phone, technically dont have to deal with this but just in case my app will be used on another deivce
- {
- Toast.makeText(getApplicationContext(), "Bluetooth Device Not Available", Toast.LENGTH_LONG).show(); //notify user
- //finish apk
- finish();
- }
- else if(!myBluetooth.isEnabled())
- {
- //Ask to the user turn the bluetooth on if the bluetooth is not already on for the phone
- Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(turnBTon,1);
- }
- btnPaired.setOnClickListener(new View.OnClickListener() { //actionlistener for the only button in this view
- @Override
- public void onClick(View v)
- {
- pairedDevicesList();
- }
- });
- }
- private void pairedDevicesList()
- {
- pairedDevices = myBluetooth.getBondedDevices();
- ArrayList list = new ArrayList(); //creating a list to store avilable devices
- if (pairedDevices.size()>0) //if there is at least 1 device avilable
- {
- for(BluetoothDevice bt : pairedDevices)
- {
- list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and its address
- }
- }
- else //no devices avilable
- {
- Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show(); //notify user
- }
- final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list); //variable to hold said 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 which is motorControl
- Intent i = new Intent(DeviceList.this, motorControl.class);
- //Change the activity to the intended activity (motorControl)
- i.putExtra(EXTRA_ADDRESS, address); //this will be received at motorControl (class) Activity
- startActivity(i);
- }
- };
- @Override
- public boolean onCreateOptionsMenu(Menu menu) //when the avilable device list is created, inflate it (pop up)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_device_list, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. (when a user selects a bluetooth device from the list)
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement