Android Open Source - bluetooth Beacon 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.beacons;
/*from   w  w w.  j  a v  a  2 s .c om*/
import java.util.ArrayList;
import java.util.List;
import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import com.mauriciotogneri.bluetooth.beacons.BeaconService.BeaconBinder;

public class BeaconManager
{
  private final Context context;
  private final Object filtersLock = new Object();
  private final List<BeaconFilter> filters = new ArrayList<BeaconFilter>();
  private final Object listenersLock = new Object();
  private final List<BeaconListener> listeners = new ArrayList<BeaconListener>();
  private final int scanFrequency;
  private boolean isConnected = false;
  private BeaconService beaconService;
  private final ServiceConnection serviceConnection;
  
  public BeaconManager(Context context, int scanFrequency) throws UnsupportedBluetoothLeException
  {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2)
    {
      throw new UnsupportedBluetoothLeException(Build.VERSION.SDK_INT);
    }
    
    this.context = context;
    this.scanFrequency = scanFrequency;
    
    this.serviceConnection = new ServiceConnection()
    {
      @Override
      public void onServiceConnected(ComponentName name, IBinder service)
      {
        onConnected(service);
      }
      
      @Override
      public void onServiceDisconnected(ComponentName name)
      {
        onDisconnected();
      }
    };
    
    enableBluetooth();
  }
  
  private void enableBluetooth()
  {
    BluetoothAdapter result = BluetoothAdapter.getDefaultAdapter();
    
    if (!result.isEnabled())
    {
      result.enable();
    }
  }
  
  public void start()
  {
    Intent intent = new Intent(this.context, BeaconService.class);
    this.context.bindService(intent, this.serviceConnection, Context.BIND_AUTO_CREATE);
  }
  
  public void pause()
  {
    if (this.beaconService != null)
    {
      this.beaconService.pause();
    }
  }
  
  public void resume()
  {
    if (this.beaconService != null)
    {
      this.beaconService.resume();
    }
  }
  
  public boolean isConnected()
  {
    return this.isConnected;
  }
  
  public void stop()
  {
    try
    {
      this.context.unbindService(this.serviceConnection);
    }
    catch (Exception e)
    {
    }
  }
  
  public void addFilter(BeaconFilter filter)
  {
    synchronized (this.filtersLock)
    {
      this.filters.add(filter);
    }
  }
  
  public void removeFilter(BeaconFilter filter)
  {
    synchronized (this.filtersLock)
    {
      this.filters.remove(filter);
    }
  }
  
  public void addListener(BeaconListener listener)
  {
    synchronized (this.listenersLock)
    {
      this.listeners.add(listener);
    }
  }
  
  public void removeListener(BeaconListener listener)
  {
    synchronized (this.listenersLock)
    {
      this.listeners.remove(listener);
    }
  }
  
  private void onConnected(IBinder service)
  {
    log("SERVICE CONNECTED");
    
    this.isConnected = true;
    
    BeaconBinder binder = (BeaconBinder)service;
    this.beaconService = binder.getService();
    
    this.beaconService.startListening(this.scanFrequency, this.filtersLock, this.filters, this.listenersLock, this.listeners);
  }
  
  private void onDisconnected()
  {
    log("SERVICE DISCONNECTED");
    
    this.isConnected = false;
  }
  
  private void log(String text)
  {
    Log.e("BEACONS_LOG", text);
  }
}




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