Created
May 3, 2016 08:59
-
-
Save ArabAgile/34c3dae64f70a78184d2654f38cb4243 to your computer and use it in GitHub Desktop.
Secure IBecaon scan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.delkontakt; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import com.kontakt.sdk.android.ble.configuration.ScanPeriod; | |
import com.kontakt.sdk.android.ble.configuration.scan.IBeaconScanContext; | |
import com.kontakt.sdk.android.ble.configuration.scan.ScanContext; | |
import com.kontakt.sdk.android.ble.connection.OnServiceReadyListener; | |
import com.kontakt.sdk.android.ble.device.BeaconRegion; | |
import com.kontakt.sdk.android.ble.discovery.BluetoothDeviceEvent; | |
import com.kontakt.sdk.android.ble.discovery.EventType; | |
import com.kontakt.sdk.android.ble.discovery.ibeacon.IBeaconDeviceEvent; | |
import com.kontakt.sdk.android.ble.manager.ProximityManager; | |
import com.kontakt.sdk.android.common.model.IProximityUUID; | |
import com.kontakt.sdk.android.common.profile.DeviceProfile; | |
import com.kontakt.sdk.android.common.profile.IBeaconRegion; | |
import com.kontakt.sdk.android.configuration.KontaktProximityManagerConfiguration; | |
import com.kontakt.sdk.android.http.HttpResult; | |
import com.kontakt.sdk.android.http.KontaktApiClient; | |
import com.kontakt.sdk.android.http.exception.ClientException; | |
import com.kontakt.sdk.android.http.interfaces.ResultApiCallback; | |
import com.kontakt.sdk.android.manager.KontaktProximityManager; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.UUID; | |
public class MainActivity extends AppCompatActivity implements ProximityManager.ProximityListener { | |
private KontaktProximityManager kontaktProximityManager; | |
private KontaktApiClient kontaktApiClient; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// KontaktProximityManagerConfiguration config = new KontaktProximityManagerConfiguration.Builder() | |
// .setCacheFileName("sc_shuffled_file") | |
// .setMonitoringEnabled(true) | |
// .setMonitoringSyncInterval(10) // 10 | |
// .setResolveInterval(5) | |
// .build(); | |
kontaktProximityManager = new KontaktProximityManager(this); | |
kontaktApiClient = new KontaktApiClient(); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
fetchProximities(); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (kontaktProximityManager.isConnected()) { | |
kontaktProximityManager.detachListener(this); | |
kontaktProximityManager.disconnect(); | |
} | |
} | |
private void fetchProximities() { | |
kontaktApiClient.listProximities(new ResultApiCallback<List<IProximityUUID>>() { | |
@Override | |
public void onSuccess(HttpResult<List<IProximityUUID>> result) { | |
if (result.isPresent()) { | |
onProximitesFetched(result.get()); | |
} | |
} | |
@Override | |
public void onFailure(ClientException e) { | |
e.printStackTrace(); | |
} | |
}); | |
} | |
private void onProximitesFetched(List<IProximityUUID> proximityUUIDs) { | |
List<IBeaconRegion> beaconRegions = new ArrayList<>(); | |
for (IProximityUUID proximityUUID : proximityUUIDs) { | |
UUID realBleProximity = proximityUUID.getSecureProximity(); | |
beaconRegions.add(new BeaconRegion.Builder().setProximity(realBleProximity).build()); | |
} | |
scanWithRegions(beaconRegions); | |
} | |
private void scanWithRegions(List<IBeaconRegion> beaconRegions) { | |
IBeaconScanContext beaconScanContext = new IBeaconScanContext.Builder() | |
.setIBeaconRegions(beaconRegions) | |
.setEventTypes(Arrays.asList(EventType.DEVICE_DISCOVERED)) | |
.build(); | |
ScanContext scanContext = new ScanContext.Builder() | |
.setScanMode(ProximityManager.SCAN_MODE_BALANCED) | |
.setScanPeriod(ScanPeriod.RANGING) | |
.setIBeaconScanContext(beaconScanContext) | |
.build(); | |
kontaktProximityManager.initializeScan(scanContext, new OnServiceReadyListener() { | |
@Override | |
public void onServiceReady() { | |
kontaktProximityManager.attachListener(MainActivity.this); | |
Log.d("AG_LOG", "onServiceReady"); | |
} | |
@Override | |
public void onConnectionFailure() { | |
Log.d("AG_LOG", "onConnectionFailure"); | |
} | |
}); | |
} | |
@Override | |
public void onScanStart() { | |
Log.d("AG_LOG", "onScanStart"); | |
} | |
@Override | |
public void onScanStop() { | |
Log.d("AG_LOG", "onScanStop"); | |
} | |
@Override | |
public void onEvent(BluetoothDeviceEvent event) { | |
if(DeviceProfile.EDDYSTONE == event.getDeviceProfile()){ | |
throw new IllegalStateException("unexpected device type"); | |
} | |
IBeaconDeviceEvent iBeaconDeviceEvent = (IBeaconDeviceEvent) event; | |
UUID proximity = iBeaconDeviceEvent.getRegion().getProximity(); | |
String uniqueId = iBeaconDeviceEvent.getDeviceList().get(0).getUniqueId(); | |
Log.d("AG_LOG", "Scanned beacon unique id="+uniqueId + " with proximity="+proximity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment