Last active
December 8, 2021 13:28
-
-
Save ToeJamson/635b14892364af30e3ad to your computer and use it in GitHub Desktop.
Android Beacon Tutorial (Building the Detector - Listener - Observer)
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
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(); | |
} |
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
public void getAdOfTheDay(int major, int minor) { | |
String channel = "YourCompany_"+major+"_"+minor; | |
try { | |
pubnub.subscribe(channel, mPubNubCallback); | |
} catch (PubnubException e) { | |
System.out.println(e.toString()); | |
} | |
} |
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
protected Callback mPubNubCallback = new Callback() { | |
@Override | |
public void successCallback(String channel, Object message) { | |
System.out.println(message.toString()); | |
} | |
}; |
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
private void setScanSettings() { | |
ScanSettings.Builder mBuilder = new ScanSettings.Builder(); | |
mBuilder.setReportDelay(0); | |
mBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER); | |
mScanSettings = mBuilder.build(); | |
} |
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
mBluetoothLeScanner.startScan(Arrays.asList(mScanFilter), mScanSettings, mScanCallback); |
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
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(); | |
} | |
} |
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
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; | |
} | |
} |
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
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"; | |
} | |
} |
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
<uses-permission android:name="android.permission.INTERNET"/> |
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
this.pubnub = new Pubnub("publish_key", "subscribe_key"); |
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
@Override | |
public void onScanResult(int callbackType, ScanResult result) { | |
[...] | |
if ((mBeaconScanRecord.getDistance() == "Immediate") && !isSubscribed()) { | |
getAdOfTheDay(mIBeaconScanRecord.getMajor(), mIBeaconScanRecord.getMinor()); | |
} | |
} |
Yes, I had this issue:
Cannot resolve method getDistance()
didRangeBeaconsInRegion(Collection beacons, Region region)
// Should be
didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region)
The example code is wrong, or the compiler they used auto-magically worked out that beacon was in a collection of Beacons.
Anybody can share the full code? the sample codes have a lot of errors and I cannot resolve method getDistance(), where did you even find this line of code didRangeBeaconsInRegion(Collection beacons, Region region) ?
Does anyone figured out the missing mess.
@ToeJamson
After a long time, can you share the source code for everyone to refer to?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Что за библиотека?