Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.voice.benz.instaremote;
- import android.app.ListActivity;
- 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.TextView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothClass;
- import android.content.BroadcastReceiver;
- import java.util.Set;
- import android.content.Intent;
- import android.content.Context;
- import java.lang.reflect.Array;
- public class MainActivity extends ActionBarActivity {
- private TextView txt_status;
- public ListView listView_newDevices, listView_pairedDevices;
- public ArrayAdapter<String> newDevicesAdapter;
- public ArrayAdapter<String> pairedDevicesAdapter;
- private Set<BluetoothDevice> pairedDevices;
- private BluetoothAdapter btAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Inizializza widgets
- txt_status = (TextView) findViewById(R.id.txt_status);
- listView_newDevices = (ListView) findViewById(R.id.listView_newDevices);
- listView_pairedDevices = (ListView) findViewById(R.id.listView_pairedDevices);
- // Inizializzazione Adapter
- ArrayAdapter<String> newDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
- ArrayAdapter<String> pairedDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
- // Assegna Adapters
- listView_newDevices.setAdapter(newDevicesAdapter);
- listView_pairedDevices.setAdapter(pairedDevicesAdapter);
- // Inizializza bluetooth adapter
- btAdapter = BluetoothAdapter.getDefaultAdapter();
- }
- // BROADCAST RECEIVER ------ GESTISCE CONNESSIONE
- 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)) {
- //BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- //do something
- }
- else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
- //do something else
- }
- }
- };
- public void find_BondedDevices (View view) {
- // Cerca bonded devices SET
- pairedDevices = btAdapter.getBondedDevices();
- if (pairedDevices.size() > 0) {
- for (BluetoothDevice device : pairedDevices) {
- String deviceBTName = device.getName();
- String deviceBTMajorClass = getBTMajorDeviceClass(device
- .getBluetoothClass()
- .getMajorDeviceClass());
- pairedDevicesAdapter.add(deviceBTName + "\n" + deviceBTMajorClass);
- }
- }
- }
- private String getBTMajorDeviceClass(int major) {
- switch (major) {
- case BluetoothClass.Device.Major.AUDIO_VIDEO:
- return "AUDIO_VIDEO";
- case BluetoothClass.Device.Major.COMPUTER:
- return "COMPUTER";
- case BluetoothClass.Device.Major.HEALTH:
- return "HEALTH";
- case BluetoothClass.Device.Major.IMAGING:
- return "IMAGING";
- case BluetoothClass.Device.Major.MISC:
- return "MISC";
- case BluetoothClass.Device.Major.NETWORKING:
- return "NETWORKING";
- case BluetoothClass.Device.Major.PERIPHERAL:
- return "PERIPHERAL";
- case BluetoothClass.Device.Major.PHONE:
- return "PHONE";
- case BluetoothClass.Device.Major.TOY:
- return "TOY";
- case BluetoothClass.Device.Major.UNCATEGORIZED:
- return "UNCATEGORIZED";
- case BluetoothClass.Device.Major.WEARABLE:
- return "AUDIO_VIDEO";
- default:
- return "unknown!";
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement