Advertisement
Guest User

Android bluetooth devices discovery

a guest
Dec 21st, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3.     private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  4.     private TextView tvBoundedDevices;
  5.     private TextView tvDiscoveredDevices;
  6.  
  7.     private IntentFilter intentFilter = new IntentFilter();
  8.     private BroadcastReceiver broadcastReceiver;
  9.     private ProgressDialog progressDialog;
  10.     private ArrayList<BluetoothDevice> bluetoothDevicesFound = new ArrayList<>();
  11.     private Set<BluetoothDevice> bluetoothDevicesBounded;
  12.  
  13.     @Override
  14.     protected void onDestroy() {
  15.         unregisterReceiver(broadcastReceiver);
  16.         bluetoothAdapter.cancelDiscovery();
  17.         super.onDestroy();
  18.     }
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.  
  25.         tvBoundedDevices = findViewById(R.id.tvBoundedDevices);
  26.         tvDiscoveredDevices = findViewById(R.id.tvDiscoveredDevices);
  27.  
  28.         intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
  29.         intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
  30.         intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
  31.         broadcastReceiver = new BroadcastReceiver() {
  32.             @Override
  33.             public void onReceive(Context context, Intent intent) {
  34.                 String action = intent.getAction();
  35.                 switch (action){
  36.                     case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
  37.                         progressDialog = ProgressDialog.show(MainActivity.this,"Attendere","Scan in corso");
  38.                         break;
  39.                     case BluetoothDevice.ACTION_FOUND:
  40.                         bluetoothDevicesFound.add((BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
  41.                         break;
  42.                     case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
  43.                         if(progressDialog.isShowing())
  44.                             progressDialog.dismiss();
  45.                         break;
  46.                     default:
  47.                         Toast.makeText(context, "Action=" + action, Toast.LENGTH_LONG).show();
  48.                 }
  49.             }
  50.         };
  51.         registerReceiver(broadcastReceiver,intentFilter);
  52.     }
  53.  
  54.     public void turnBluetoothOn(View view){
  55.         if(!bluetoothAdapter.isEnabled()){
  56.             bluetoothAdapter.enable();
  57.             Toast.makeText(this,"Bluetooth attivato",Toast.LENGTH_SHORT).show();
  58.         }
  59.     }
  60.  
  61.     public void turnBluetoothOff(View view){
  62.         if(bluetoothAdapter.isEnabled()){
  63.             bluetoothAdapter.disable();
  64.             Toast.makeText(this,"Bluetooth disattivato",Toast.LENGTH_SHORT).show();
  65.         }
  66.     }
  67.  
  68.     public void scanDevices(View view){
  69.         turnBluetoothOn(null);
  70.         if(bluetoothAdapter.isDiscovering())
  71.             bluetoothAdapter.cancelDiscovery();
  72.         Toast.makeText(this, "Scansione " + (bluetoothAdapter.startDiscovery()?"":"non") + " avviata.", Toast.LENGTH_SHORT).show();
  73.         printDevices();
  74.     }
  75.  
  76.     String toStamp = "";
  77.     private void printDevices(){
  78.         bluetoothDevicesBounded = bluetoothAdapter.getBondedDevices();
  79.         if(!bluetoothDevicesBounded.isEmpty()){
  80.             toStamp = "Dispositivi associati:\n";
  81.             for(BluetoothDevice b : bluetoothDevicesBounded){
  82.                 toStamp += b.getName() + " | " + b.getAddress() + "\n";
  83.             }
  84.             tvBoundedDevices.setText(toStamp + "");
  85.         }
  86.         if(!bluetoothDevicesFound.isEmpty()){
  87.             toStamp = "Dispositivi trovati:\n";
  88.             for(BluetoothDevice b : bluetoothDevicesFound){
  89.                 toStamp += b.getName() + " | " + b.getAddress() + "\n";
  90.             }
  91.             tvDiscoveredDevices.setText(toStamp + "");
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement