Android Open Source - bluetooth Scanner Manager






From Project

Back to project page bluetooth.

License

The source code is released under:

MIT License

If you think the Android project bluetooth listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mauriciotogneri.bluetooth.connection.scan;
// w  w w  .  j  av a 2s.c  o  m
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class ScannerManager
{
  private final Context context;
  private final DeviceScanner deviceScanner;
  private final BluetoothAdapter bluetoothAdapter;
  private final BroadcastReceiver receiver;
  
  public ScannerManager(Context context, DeviceScanner deviceScanner)
  {
    this.context = context;
    this.deviceScanner = deviceScanner;
    this.bluetoothAdapter = getBluetoothAdapter();
    
    this.receiver = new BroadcastReceiver()
    {
      @Override
      public void onReceive(Context context, Intent intent)
      {
        if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction()))
        {
          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
          onDeviceDiscovered(device);
        }
      }
    };
  }
  
  private BluetoothAdapter getBluetoothAdapter()
  {
    BluetoothAdapter result = BluetoothAdapter.getDefaultAdapter();
    
    if (!result.isEnabled())
    {
      result.enable();
    }
    
    return result;
  }
  
  public void scan(boolean includePaired)
  {
    if (includePaired)
    {
      Set<BluetoothDevice> pairedDevices = this.bluetoothAdapter.getBondedDevices();
      
      for (BluetoothDevice device : pairedDevices)
      {
        onDeviceDiscovered(device);
      }
    }
    
    if (!this.bluetoothAdapter.isDiscovering())
    {
      IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
      this.context.registerReceiver(this.receiver, filter);
      
      this.bluetoothAdapter.startDiscovery();
    }
  }
  
  public void scan()
  {
    scan(false);
  }
  
  private void onDeviceDiscovered(BluetoothDevice device)
  {
    this.deviceScanner.onDeviceDiscovered(device);
  }
  
  public void stop()
  {
    if (this.bluetoothAdapter.isDiscovering())
    {
      this.bluetoothAdapter.cancelDiscovery();
    }
    
    try
    {
      this.context.unregisterReceiver(this.receiver);
    }
    catch (IllegalArgumentException e)
    {
    }
  }
}




Java Source Code List

com.mauriciotogneri.bluetooth.beacons.BeaconFilter.java
com.mauriciotogneri.bluetooth.beacons.BeaconListener.java
com.mauriciotogneri.bluetooth.beacons.BeaconManager.java
com.mauriciotogneri.bluetooth.beacons.BeaconService.java
com.mauriciotogneri.bluetooth.beacons.Beacon.java
com.mauriciotogneri.bluetooth.beacons.UnsupportedBluetoothLeException.java
com.mauriciotogneri.bluetooth.connection.client.ClientConnection.java
com.mauriciotogneri.bluetooth.connection.client.ClientEvent.java
com.mauriciotogneri.bluetooth.connection.client.ClientLink.java
com.mauriciotogneri.bluetooth.connection.client.ClientThread.java
com.mauriciotogneri.bluetooth.connection.kernel.ConnectionThread.java
com.mauriciotogneri.bluetooth.connection.scan.DeviceScanner.java
com.mauriciotogneri.bluetooth.connection.scan.ScannerManager.java
com.mauriciotogneri.bluetooth.connection.server.ServerConnection.java
com.mauriciotogneri.bluetooth.connection.server.ServerEvent.java
com.mauriciotogneri.bluetooth.connection.server.ServerLink.java
com.mauriciotogneri.bluetooth.connection.server.ServerThread.java