-
-
Save stephenlb/75190ba1a6274b5146c10c901793ee79 to your computer and use it in GitHub Desktop.
How to Build an Android Beacon (iBeacon) Emitter [3/3]
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 void setAdvertiseData() { | |
AdvertiseData.Builder mBuilder = new AdvertiseData.Builder() | |
ByteBuffer mManufacturerData = ByteBuffer.allocate(24); | |
byte[] uuid = getIdAsByte(UUID.fromString("0CF052C297CA407C84F8B62AAC4E9020")); | |
mManufacturerData.put(0, (byte)0xBE); // Beacon Identifier | |
mManufacturerData.put(1, (byte)0xAC); // Beacon Identifier | |
for (int i=2; i<=17; i++) { | |
mManufacturerData.put(i, uuid[i-2]); // adding the UUID | |
} | |
mManufacturerData.put(18, (byte)0x00); // first byte of Major | |
mManufacturerData.put(19, (byte)0x09); // second byte of Major | |
mManufacturerData.put(20, (byte)0x00); // first minor | |
mManufacturerData.put(21, (byte)0x06); // second minor | |
mManufacturerData.put(22, (byte)0xB5); // txPower | |
mBuilder.addManufacturerData(224, mManufacturerData.array()); // using google's company ID | |
mAdvertiseData = 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
protected void setAdvertiseSettings() { | |
AdvertiseSettings.Builder mBuilder = new AdvertiseSettings.Builder(); | |
mBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER); | |
mBuilder.setConnectable(false); | |
mBuilder.setTimeout(0); | |
mBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM); | |
mAdvertiseSettings = 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
mBluetoothLeAdvertiser.startAdvertising(mAdvertiseSettings, mAdvertiseData, mAdvertiseCallback); |
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
PNConfiguration pnConfiguration = new PNConfiguration(); | |
pnConfiguration.setSubscribeKey("subscribe_key"); | |
pnConfiguration.setPublishKey("publish_key"); | |
pnConfiguration.setSecure(false); | |
PubNub pubnub = new PubNub(pnConfiguration); |
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
String channel = "YourCompany_"+major+"_"+minor; | |
try { | |
pubnub.presence(channel, mPresenceCallback); | |
} catch (PubnubException e) { | |
Log.d("PUBNUB",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 mPresenceCallback = new Callback() { | |
@Override | |
public void successCallback(String channel, Object message) { | |
pubnub.publish() | |
.message(message) | |
.channel(channel) | |
.async(new PNCallback<PNPublishResult>() { | |
@Override | |
public void onResponse(PNPublishResult result, PNStatus status) { | |
// handle publish result, status always present, result if successful | |
// status.isError to see if error happened | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment