Android Open Source - bluetooth Beacon Service






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 av  a2  s  .c o m
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.TargetApi;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter.LeScanCallback;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

@TargetApi(18)
public class BeaconService extends Service implements LeScanCallback
{
  private int scanFrequency;
  private Object filtersLock;
  private List<BeaconFilter> filters;
  private Object listenersLock;
  private List<BeaconListener> listeners;
  private BluetoothAdapter bluetoothAdapter;
  private volatile boolean scanningActive = false;
  private final Handler handler = new Handler();
  private final Object currentBeaconsLock = new Object();
  private final Map<String, Beacon> currentBeacons = new HashMap<String, Beacon>();
  
  public void startListening(int scanFrequency, Object filtersLock, List<BeaconFilter> filters, Object listenersLock, List<BeaconListener> listeners)
  {
    this.scanFrequency = scanFrequency;
    
    this.filtersLock = filtersLock;
    this.filters = filters;
    
    this.listenersLock = listenersLock;
    this.listeners = listeners;
    
    this.scanningActive = true;
    
    BluetoothManager bluetoothManager = (BluetoothManager)getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
    this.bluetoothAdapter = bluetoothManager.getAdapter();
    this.bluetoothAdapter.startLeScan(this);
    
    startScanningCycle();
  }
  
  public void pause()
  {
    this.scanningActive = false;
  }
  
  public void resume()
  {
    this.scanningActive = true;
  }
  
  private void startScanningCycle()
  {
    if (this.scanningActive)
    {
      this.handler.postDelayed(new Runnable()
      {
        @Override
        public void run()
        {
          finishScanningCycle();
          startScanningCycle();
        }
      }, this.scanFrequency);
    }
  }
  
  @Override
  public void onLeScan(BluetoothDevice device, int rssi, byte[] data)
  {
    String macAddress = device.getAddress();
    
    log("BEACON SCANNED: " + macAddress + ", RSSI: " + rssi + ", DATA LENGTH: " + Arrays.toString(data));
    
    synchronized (this.filtersLock)
    {
      for (BeaconFilter filter : this.filters)
      {
        Beacon beacon = filter.getBeacon(macAddress, rssi, data);
        
        if (beacon != null)
        {
          synchronized (this.currentBeaconsLock)
          {
            this.currentBeacons.put(macAddress, beacon);
          }
          
          break;
        }
      }
    }
  }
  
  private void finishScanningCycle()
  {
    if (this.scanningActive)
    {
      log("FINISHED SCANNING CYCLE");
      
      List<Beacon> beaconsList = new ArrayList<Beacon>();
      
      synchronized (this.currentBeaconsLock)
      {
        Collection<Beacon> beacons = this.currentBeacons.values();
        
        for (Beacon beacon : beacons)
        {
          beaconsList.add(beacon);
        }
        
        this.currentBeacons.clear();
      }
      
      synchronized (this.listenersLock)
      {
        for (BeaconListener listener : this.listeners)
        {
          listener.onReceive(beaconsList);
        }
      }
    }
  }
  
  public class BeaconBinder extends Binder
  {
    public BeaconService getService()
    {
      return BeaconService.this;
    }
  }
  
  @Override
  public IBinder onBind(Intent intent)
  {
    return new BeaconBinder();
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId)
  {
    return Service.START_STICKY;
  }
  
  @Override
  public void onDestroy()
  {
    log("SERVICE DESTROYED");
    
    this.scanningActive = false;
    
    if (this.bluetoothAdapter != null)
    {
      this.bluetoothAdapter.stopLeScan(this);
    }
    
    super.onDestroy();
  }
  
  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