Skip to content

Instantly share code, notes, and snippets.

@domadev812
Forked from ToeJamson/1.java
Last active November 15, 2017 21:05
Show Gist options
  • Select an option

  • Save domadev812/ab2ff98b5999cfd177cd0eb6f940f7be to your computer and use it in GitHub Desktop.

Select an option

Save domadev812/ab2ff98b5999cfd177cd0eb6f940f7be to your computer and use it in GitHub Desktop.
Android Beacon Tutorial (Building the Detector - Listener - Observer)
private void setScanFilter() {
ScanFilter.Builder mBuilder = new ScanFilter.Builder();
ByteBuffer mManufacturerData = ByteBuffer.allocate(23);
ByteBuffer mManufacturerDataMask = ByteBuffer.allocate(24);
byte[] uuid = getIdAsByte(UUID.fromString("0CF052C297CA407C84F8B62AAC4E9020");
mManufacturerData.put(0, (byte)0xBE);
mManufacturerData.put(1, (byte)0xAC);
for (int i=2; i<=17; i++) {
mManufacturerData.put(i, uuid[i-2]);
}
for (int i=0; i<=17; i++) {
mManufacturerDataMask.put((byte)0x01);
}
mBuilder.setManufacturerData(224, mManufacturerData.array(), mManufacturerDataMask.array());
mScanFilter = mBuilder.build();
}
public void getAdOfTheDay(int major, int minor) {
try {
pubnub.subscribe()
.channels(Arrays.asList("my_channel")) // subscribe to channels
.execute();
} catch (PubnubException e) {
System.out.println(e.toString());
}
}
SubscribeCallback subscribeCallback = new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
}
@Override
public void message(PubNub pubnub, PNMessageResult message) {
}
@Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
}
};
pubnub.addListener(subscribeCallback);
private void setScanSettings() {
ScanSettings.Builder mBuilder = new ScanSettings.Builder();
mBuilder.setReportDelay(0);
mBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
mScanSettings = mBuilder.build();
}
mBluetoothLeScanner.startScan(Arrays.asList(mScanFilter), mScanSettings, mScanCallback);
protected ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
ScanRecord mScanRecord = result.getScanRecord();
int[] manufacturerData = mScanRecord.getManufacturerSpecificData(224);
int mRssi = result.getRssi();
}
}
public double calculateDistance(int txPower, double rssi) {
if (rssi == 0) {
return -1.0; // if we cannot determine accuracy, return -1.
}
double ratio = rssi*1.0/txPower;
if (ratio < 1.0) {
return Math.pow(ratio,10);
}
else {
double accuracy = (0.89976)*Math.pow(ratio,7.7095) + 0.111;
return accuracy;
}
}
private String getDistance(accuracy) {
if (accuracy == -1.0) {
return "Unknown";
} else if (accuracy < 1) {
return "Immediate";
} else if (accuracy < 3) {
return "Near";
} else {
return "Far";
}
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.setSubscribeKey("demo");
pnConfiguration.setPublishKey("demo");
PubNub pubnub = new PubNub(pnConfiguration);
@Override
public void onScanResult(int callbackType, ScanResult result) {
[...]
if ((mBeaconScanRecord.getDistance() == "Immediate") && !isSubscribed()) {
getAdOfTheDay(mIBeaconScanRecord.getMajor(), mIBeaconScanRecord.getMinor());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment