0

I need my app to get notified when it is in range of a specific BLE device. Once in range, I want to connect to the device and send data to it.

Approach 1:
Periodic BLE scans at some interval (i.e. every 30 seconds). However, this would not be good for battery. I can improve this a bit by only scanning when the device location is near the location of the BLE device (via Android Geofence APIs). But still, not great for battery.

Approach 2:
Use Android Awareness API. This seems like a great candidate. However, it seems you are forced to register your BLE device with Google as a BLE beacon. Does anyone know if this is an absolute requirement? I do not want to register it with Google.

Ivan Stalev
  • 303
  • 1
  • 3
  • 11

2 Answers2

0

Have a look into the Android Altbeacon library. It can do what you need with battery saving features.

Regarding approach 1, sample code from the library below.

public class MonitoringActivity extends Activity implements BeaconConsumer {
    protected static final String TAG = "MonitoringActivity";
    private BeaconManager beaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranging);
                beaconManager = BeaconManager.getInstanceForApplication(this);
        // To detect proprietary beacons, you must add a line like below corresponding to your beacon
        // type.  Do a web search for "setBeaconLayout" to get the proper expression.
        // beaconManager.getBeaconParsers().add(new BeaconParser().
        //        setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
        beaconManager.bind(this);
    }
    @Override 
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }
    @Override
    public void onBeaconServiceConnect() {
        beaconManager.removeAllMonitorNotifiers();
        beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            Log.i(TAG, "I just saw an beacon for the first time!");        
        }

        @Override
        public void didExitRegion(Region region) {
            Log.i(TAG, "I no longer see an beacon");
        }

        @Override
            public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);        
            }
        });

        try {
            beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
        } catch (RemoteException e) {    }
    }

}

g2server
  • 5,037
  • 3
  • 29
  • 40
0

Why don't you just use https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context,%20boolean,%20android.bluetooth.BluetoothGattCallback,%20int,%20int) with autoConnect=true? The duty cycle of the radio is like 4% so it hardly consumes any battery.

Emil
  • 16,784
  • 2
  • 41
  • 52