Advertisement
Guest User

JLechman DeviceList.java mod

a guest
Oct 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1. package com.led.GUI;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.Button;
  12. import android.widget.ListView;
  13. import android.bluetooth.BluetoothAdapter;
  14. import android.bluetooth.BluetoothDevice;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.Set;
  20.  
  21.  
  22. public class DeviceList extends ActionBarActivity   //main class
  23. {
  24.     //GUI objects
  25.     Button btnPaired;
  26.     ListView devicelist;
  27.     //Bluetooth instance variables
  28.     private BluetoothAdapter myBluetooth = null;
  29.     private Set<BluetoothDevice> pairedDevices;
  30.     public static String EXTRA_ADDRESS = "device_address";
  31.  
  32.     @Override
  33.     protected void onCreate(Bundle savedInstanceState)  //when the GUI instance (activity_device_list) is created
  34.     {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.activity_device_list); //set the current view to device list xml
  37.  
  38.         //initializing GUI objects, basically linking them with the xml file objects
  39.         btnPaired = (Button)findViewById(R.id.button);
  40.         devicelist = (ListView)findViewById(R.id.listView);
  41.  
  42.         //if the phone has bluetooth, get the adapter
  43.         myBluetooth = BluetoothAdapter.getDefaultAdapter();
  44.  
  45.         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
  46.         {
  47.  
  48.             Toast.makeText(getApplicationContext(), "Bluetooth Device Not Available", Toast.LENGTH_LONG).show();    //notify user
  49.  
  50.             //finish apk
  51.             finish();
  52.         }
  53.         else if(!myBluetooth.isEnabled())
  54.         {
  55.                 //Ask to the user turn the bluetooth on if the bluetooth is not already on for the phone
  56.                 Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  57.                 startActivityForResult(turnBTon,1);
  58.         }
  59.  
  60.         btnPaired.setOnClickListener(new View.OnClickListener() {   //actionlistener for the only button in this view
  61.             @Override
  62.             public void onClick(View v)
  63.             {
  64.                 pairedDevicesList();
  65.             }
  66.         });
  67.  
  68.     }
  69.  
  70.     private void pairedDevicesList()
  71.     {
  72.         pairedDevices = myBluetooth.getBondedDevices();
  73.         ArrayList list = new ArrayList();   //creating a list to store avilable devices
  74.  
  75.         if (pairedDevices.size()>0) //if there is at least 1 device avilable
  76.         {
  77.             for(BluetoothDevice bt : pairedDevices)
  78.             {
  79.                 list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and its address
  80.             }
  81.         }
  82.         else    //no devices avilable
  83.         {
  84.             Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show();    //notify user
  85.         }
  86.  
  87.         final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);      //variable to hold said list
  88.         devicelist.setAdapter(adapter);
  89.         devicelist.setOnItemClickListener(myListClickListener); //Method called when the device from the list is clicked
  90.  
  91.     }
  92.  
  93.     private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener()
  94.     {
  95.         public void onItemClick (AdapterView<?> av, View v, int arg2, long arg3)
  96.         {
  97.             // Get the device MAC address, the last 17 chars in the View
  98.             String info = ((TextView) v).getText().toString();
  99.             String address = info.substring(info.length() - 17);
  100.  
  101.             // Make an intent to start next activity which is motorControl
  102.             Intent i = new Intent(DeviceList.this, motorControl.class);
  103.  
  104.             //Change the activity to the intended activity (motorControl)
  105.             i.putExtra(EXTRA_ADDRESS, address); //this will be received at motorControl (class) Activity
  106.             startActivity(i);
  107.         }
  108.     };
  109.  
  110.  
  111.     @Override
  112.     public boolean onCreateOptionsMenu(Menu menu)   //when the avilable device list is created, inflate it (pop up)
  113.     {
  114.         // Inflate the menu; this adds items to the action bar if it is present.
  115.         getMenuInflater().inflate(R.menu.menu_device_list, menu);
  116.         return true;
  117.     }
  118.  
  119.     @Override
  120.     public boolean onOptionsItemSelected(MenuItem item) {
  121.         // Handle action bar item clicks here. (when a user selects a bluetooth device from the list)
  122.         int id = item.getItemId();
  123.  
  124.         if (id == R.id.action_settings) {
  125.             return true;
  126.         }
  127.  
  128.         return super.onOptionsItemSelected(item);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement