C Forge
C Forge

Reputation: 240

How to set iBeacon message id (UUID, Major, Minor) in android (for own mobile device) programatically?

My app has to broadcast via Bluetooth an iBeacon message. The iBeacon message should be fixed in a particular UUID, Major and Minor.

How can it be done without using any 3rd party applications? (I'm okay with libraries, I just don't want the user to need another app to use this app. I prefer this app to be self-dependent when being used).

EDIT : I've built it with only google ble docs and any other libraries haven't been used. Though, I'm open to implement any suggestions that make it work.

Upvotes: 0

Views: 521

Answers (1)

davidgyoung
davidgyoung

Reputation: 64916

The easiest way to do this is to use the Android Beacon Library which is totally free and open source.

  1. Add this to your build.gradle dependencies:

     dependencies {
        implementation 'org.altbeacon:android-beacon-library:2+'
     }
    
  2. Paste this code to start your transmitter:

     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);
    

If you really want to not compile against a third-party library, you are welcome to copy the source code for the BeaconTransmitter, but that's harder to do:

https://github.com/AltBeacon/android-beacon-library/blob/master/src/main/java/org/altbeacon/beacon/BeaconTransmitter.java

Upvotes: 1

Related Questions