Tuesday, 9 June 2020

Bluetooth Low Energy Advertising

Hi Guys !!! Hope all is well
Today I am going to discus Bluetooth Low Energy (BLE) in Android.

  • Bluetooth Low Energy (BLE), available in Android 4.3 and later
  • Creates short connections between devices to transfer bursts of data
  • Bluetooth Low Energy (BLE) conserves power by remaining in sleep mode most of the time
  • It wakes up only to make advertisements and short connections
This lets BLE provide lower bandwidth and reduced power consumption compared to Classic Bluetooth.

In this tutorial, We will learn about the BluetoothLeAdvertiser class,
which enables developers to turn a supported phone into a Bluetooth LE beacon
without the need for additional hardware.

 



You can download complete project from my GitHub page 
    private void advertise() {
            mBluetoothAdvertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
            AdvertiseSettings settings = new AdvertiseSettings.Builder()
                    .setAdvertiseMode(mode)
                    .setTxPowerLevel(power)
                    .setTimeout(3*60*1000)
                    .setConnectable(true)
                    .build();

            ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) );

            AdvertiseData data = new AdvertiseData.Builder()
                    .setIncludeDeviceName( true )
                    //.addServiceUuid( pUuid )           
          .addServiceData( pUuid, "SK".getBytes(Charset.forName("UTF-8") ) )
                    .build();

            advertisingCallback = new AdvertiseCallback() {
                @Override                
  public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                    Log.e( "Saurabh_BLE", "Advertising onStartSuccess: getTxPowerLevel "                            
            + settingsInEffect.getTxPowerLevel() );
                    String modes = "";
                    if(2 == settingsInEffect.getMode()){
                        modes = "LOW LATENCY Mode";
                    }else if(1 == settingsInEffect.getMode()) {
                        modes = "BALANCED Mode";
                    }else if(0 == settingsInEffect.getMode()) {
                        modes = "LOW POWER Mode";
                    }

                    String power = "";
                    if(3 == settingsInEffect.getTxPowerLevel()){
                        power = "TX_POWER_HIGH";
                    }else if(2 == settingsInEffect.getTxPowerLevel()) {
                        power = "TX_POWER_MEDIUM";
                    }else if(1 == settingsInEffect.getTxPowerLevel()) {
                        power = "TX_POWER_LOW ";
                    } else if(0 == settingsInEffect.getTxPowerLevel()) {
                        power = "TX_POWER_ULTRA_LOW";
                    }

                    mText.setText("Advertising Start: Success \n"+
                            "Advertising PowerLevel: "+power+"\n"+
                            "Advertising Mode: "+modes+" \n"+
                            "Advertising Timeout(ms): "+settingsInEffect.getTimeout()+"\n"+
                            "Advertising Connectable: "+settingsInEffect.isConnectable());
                    super.onStartSuccess(settingsInEffect);
                }

                @Override                
             public void onStartFailure(int errorCode) {
                    Log.e( "Saurabh_BLE", "Advertising onStartFailure: " + errorCode );
                    mText.setText("Advertising Start Failure ErrorCode: "+errorCode);
                    super.onStartFailure(errorCode);
                }
            };

        mBluetoothAdvertiser.startAdvertising( settings, data, advertisingCallback );
    }


Thanks
Saurabh 
Happy Coding !!!

No comments:

Post a Comment

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...