public void startAdvertising ( ) { if ( mBeacon == null ) { throw new NullPointerException ( "Beacon cannot be null. Set beacon before starting advertising" ) ; } int manufacturerCode = mBeacon . getManufacturer ( ) ; int serviceUuid = - 1 ; if ( mBeaconParser . getServiceUuid ( ) != null ) { serviceUuid = mBeaconParser . getServiceUuid ( ) . intValue ( ) ; } if ( mBeaconParser == null ) { throw new NullPointerException ( "You must supply a BeaconParser instance to BeaconTransmitter." ) ; } byte [ ] advertisingBytes = mBeaconParser . getBeaconAdvertisementData ( mBeacon ) ; String byteString = "" ; for ( int i = 0 ; i < advertisingBytes . length ; i ++ ) { byteString += String . format ( "%02X" , advertisingBytes [ i ] ) ; byteString += " " ; } LogManager . d ( TAG , "Starting advertising with ID1: %s ID2: %s ID3: %s and data: %s of size " + "%s" , mBeacon . getId1 ( ) , mBeacon . getIdentifiers ( ) . size ( ) > 1 ? mBeacon . getId2 ( ) : "" , mBeacon . getIdentifiers ( ) . size ( ) > 2 ? mBeacon . getId3 ( ) : "" , byteString , advertisingBytes . length ) ; try { AdvertiseData . Builder dataBuilder = new AdvertiseData . Builder ( ) ; if ( serviceUuid > 0 ) { byte [ ] serviceUuidBytes = new byte [ ] { ( byte ) ( serviceUuid & 0xff ) , ( byte ) ( ( serviceUuid >> 8 ) & 0xff ) } ; ParcelUuid parcelUuid = parseUuidFrom ( serviceUuidBytes ) ; dataBuilder . addServiceData ( parcelUuid , advertisingBytes ) ; dataBuilder . addServiceUuid ( parcelUuid ) ; dataBuilder . setIncludeTxPowerLevel ( false ) ; dataBuilder . setIncludeDeviceName ( false ) ; } else { dataBuilder . addManufacturerData ( manufacturerCode , advertisingBytes ) ; } AdvertiseSettings . Builder settingsBuilder = new AdvertiseSettings . Builder ( ) ; settingsBuilder . setAdvertiseMode ( mAdvertiseMode ) ; settingsBuilder . setTxPowerLevel ( mAdvertiseTxPowerLevel ) ; settingsBuilder . setConnectable ( mConnectable ) ; mBluetoothLeAdvertiser . startAdvertising ( settingsBuilder . build ( ) , dataBuilder . build ( ) , getAdvertiseCallback ( ) ) ; LogManager . d ( TAG , "Started advertisement with callback: %s" , getAdvertiseCallback ( ) ) ; } catch ( Exception e ) { LogManager . e ( e , TAG , "Cannot start advertising due to exception" ) ; } }
Starts this beacon advertising