Advertisement
Guest User

complete code casting exception on arrayadapter add

a guest
May 9th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 KB | None | 0 0
  1. package com.voice.benz.instaremote;
  2.  
  3. import android.app.ListActivity;
  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.TextView;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.ListView;
  12. import android.bluetooth.BluetoothAdapter;
  13. import android.bluetooth.BluetoothDevice;
  14. import android.bluetooth.BluetoothClass;
  15. import android.content.BroadcastReceiver;
  16.  
  17. import java.util.Set;
  18.  
  19. import android.content.Intent;
  20. import android.content.Context;
  21.  
  22. import java.lang.reflect.Array;
  23.  
  24.  
  25. public class MainActivity extends ActionBarActivity {
  26.  
  27.     private TextView txt_status;
  28.     public ListView listView_newDevices, listView_pairedDevices;
  29.     public ArrayAdapter<String> newDevicesAdapter;
  30.     public ArrayAdapter<String> pairedDevicesAdapter;
  31.     private Set<BluetoothDevice> pairedDevices;
  32.  
  33.     private BluetoothAdapter btAdapter;
  34.  
  35.  
  36.     @Override
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.activity_main);
  40.  
  41.         // Inizializza widgets
  42.         txt_status = (TextView) findViewById(R.id.txt_status);
  43.         listView_newDevices = (ListView) findViewById(R.id.listView_newDevices);
  44.         listView_pairedDevices = (ListView) findViewById(R.id.listView_pairedDevices);
  45.  
  46.         // Inizializzazione Adapter

  47.  
  48.         ArrayAdapter<String> newDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
  49.         ArrayAdapter<String> pairedDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
  50.  
  51.         // Assegna Adapters

  52.  
  53.         listView_newDevices.setAdapter(newDevicesAdapter);
  54.         listView_pairedDevices.setAdapter(pairedDevicesAdapter);
  55.  
  56.         // Inizializza bluetooth adapter
  57.  
  58.         btAdapter = BluetoothAdapter.getDefaultAdapter();
  59.  
  60.  
  61.  
  62.     }
  63.  
  64.     // BROADCAST RECEIVER ------ GESTISCE CONNESSIONE


  65.  
  66.     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  67.         @Override
  68.         public void onReceive(Context context, Intent intent) {
  69.             String action = intent.getAction();
  70.  
  71.             // When discovery finds a device
  72.             if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  73.                 //BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  74.  
  75.                 //do something
  76.             }
  77.  
  78.             else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
  79.                 //do something else
  80.             }
  81.         }
  82.     };
  83.  
  84.     public void find_BondedDevices (View view) {
  85.  
  86.         // Cerca bonded devices SET
  87.         pairedDevices = btAdapter.getBondedDevices();
  88.  
  89.         if (pairedDevices.size() > 0) {
  90.             for (BluetoothDevice device : pairedDevices) {
  91.                 String deviceBTName = device.getName();
  92.                 String deviceBTMajorClass = getBTMajorDeviceClass(device
  93.                         .getBluetoothClass()
  94.                         .getMajorDeviceClass());
  95.                 pairedDevicesAdapter.add(deviceBTName + "\n" + deviceBTMajorClass);
  96.             }
  97.         }
  98.  
  99.  
  100.  
  101.     }
  102.  
  103.     private String getBTMajorDeviceClass(int major) {
  104.         switch (major) {
  105.             case BluetoothClass.Device.Major.AUDIO_VIDEO:
  106.                 return "AUDIO_VIDEO";
  107.             case BluetoothClass.Device.Major.COMPUTER:
  108.                 return "COMPUTER";
  109.             case BluetoothClass.Device.Major.HEALTH:
  110.                 return "HEALTH";
  111.             case BluetoothClass.Device.Major.IMAGING:
  112.                 return "IMAGING";
  113.             case BluetoothClass.Device.Major.MISC:
  114.                 return "MISC";
  115.             case BluetoothClass.Device.Major.NETWORKING:
  116.                 return "NETWORKING";
  117.             case BluetoothClass.Device.Major.PERIPHERAL:
  118.                 return "PERIPHERAL";
  119.             case BluetoothClass.Device.Major.PHONE:
  120.                 return "PHONE";
  121.             case BluetoothClass.Device.Major.TOY:
  122.                 return "TOY";
  123.             case BluetoothClass.Device.Major.UNCATEGORIZED:
  124.                 return "UNCATEGORIZED";
  125.             case BluetoothClass.Device.Major.WEARABLE:
  126.                 return "AUDIO_VIDEO";
  127.             default:
  128.                 return "unknown!";
  129.         }
  130.     }
  131.  
  132.  
  133.     @Override
  134.     public boolean onCreateOptionsMenu(Menu menu) {
  135.         // Inflate the menu; this adds items to the action bar if it is present.
  136.         getMenuInflater().inflate(R.menu.menu_main, menu);
  137.         return true;
  138.     }
  139.  
  140.     @Override
  141.     public boolean onOptionsItemSelected(MenuItem item) {
  142.         // Handle action bar item clicks here. The action bar will
  143.         // automatically handle clicks on the Home/Up button, so long
  144.         // as you specify a parent activity in AndroidManifest.xml.
  145.         int id = item.getItemId();
  146.  
  147.         //noinspection SimplifiableIfStatement
  148.         if (id == R.id.action_settings) {
  149.             return true;
  150.         }
  151.  
  152.         return super.onOptionsItemSelected(item);
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement