-
-
Save domadev812/ab2ff98b5999cfd177cd0eb6f940f7be to your computer and use it in GitHub Desktop.
Android Beacon Tutorial (Building the Detector - Listener - Observer)
This file contains hidden or 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 hidden or 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) { | |
| try { | |
| pubnub.subscribe() | |
| .channels(Arrays.asList("my_channel")) // subscribe to channels | |
| .execute(); | |
| } catch (PubnubException e) { | |
| System.out.println(e.toString()); | |
| } | |
| } |
This file contains hidden or 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
| 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); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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" /> | |
| <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> |
This file contains hidden or 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
| PNConfiguration pnConfiguration = new PNConfiguration(); | |
| pnConfiguration.setSubscribeKey("demo"); | |
| pnConfiguration.setPublishKey("demo"); | |
| PubNub pubnub = new PubNub(pnConfiguration); |
This file contains hidden or 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()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment