Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Forked from domadev812/1.java
Created November 15, 2017 21:06
Show Gist options
  • Save stephenlb/75190ba1a6274b5146c10c901793ee79 to your computer and use it in GitHub Desktop.
Save stephenlb/75190ba1a6274b5146c10c901793ee79 to your computer and use it in GitHub Desktop.
How to Build an Android Beacon (iBeacon) Emitter [3/3]
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();
}
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();
}
mBluetoothLeAdvertiser.startAdvertising(mAdvertiseSettings, mAdvertiseData, mAdvertiseCallback);
<uses-permission android:name="android.permission.INTERNET"/>
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.setSubscribeKey("subscribe_key");
pnConfiguration.setPublishKey("publish_key");
pnConfiguration.setSecure(false);
PubNub pubnub = new PubNub(pnConfiguration);
String channel = "YourCompany_"+major+"_"+minor;
try {
pubnub.presence(channel, mPresenceCallback);
} catch (PubnubException e) {
Log.d("PUBNUB",e.toString());
}
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