Skip to content

Instantly share code, notes, and snippets.

@davidgyoung
Created September 11, 2018 21:15
Show Gist options
  • Save davidgyoung/111d9b295a6df3a0c0acc1faaedf435f to your computer and use it in GitHub Desktop.
Save davidgyoung/111d9b295a6df3a0c0acc1faaedf435f to your computer and use it in GitHub Desktop.
How to make Android app transmit as a beacon
package com.davidgyoungtech.instantapptransmitter.feature;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseSettings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.BeaconTransmitter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Beacon beacon = new Beacon.Builder()
.setId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")
.setId2("1")
.setId3("2")
.setManufacturer(0x004c)
.setTxPower(-59)
.build();
BeaconParser beaconParser = new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser);
beaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
((TextView)findViewById(R.id.transmitterStatus)).setText("Advertising iBeacon identifiers: "+beacon.toString());
}
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
((TextView)findViewById(R.id.transmitterStatus)).setText("Failed to start advertising, code: "+errorCode);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment