#StackBounty: #android #android-studio #bluetooth-lowenergy #signals #packet How to track the packets transmitted in Bluetooth Low Ener…

Bounty: 50

I am making an Android app for transmitting and scanning BLE signals for an experiment. I need the metric for how many packets are advertised by the transmitting device and how many are received by the scanning device. Is it possible to obtain these numbers?

Here is my advertisement code :

private void transmit() {   

        disableScanBtn();

        mBluetoothLeAdvertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();



        switch (advParameter){
            case 0 :
                advInterval = AdvertisingSetParameters.INTERVAL_HIGH;
                advSetting = "High";
                break;

            case 1 :
                advInterval = AdvertisingSetParameters.INTERVAL_MEDIUM;
                advSetting = "Medium";
                break;
            case 2 :
                advInterval = AdvertisingSetParameters.INTERVAL_LOW;
                advSetting = "Low";
                break;
            case 3 :

                advInterval = AdvertisingSetParameters.INTERVAL_MIN;
                advSetting = "Min";
                break;
            default:
                advInterval = AdvertisingSetParameters.INTERVAL_MAX;
                advSetting = "Max";
        }

        AdvertisingSetParameters parameters = new AdvertisingSetParameters.Builder()
                .setInterval(advInterval)
                .setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
                .setConnectable(true)
                .build();

        deviceId = Installation.id ( this );
        ParcelUuid pUuid = new ParcelUuid( UUID.fromString( deviceId ) );
        AdvertiseData data = new AdvertiseData.Builder()
                .setIncludeDeviceName(true)
                .addServiceUuid( pUuid )
                .addServiceData( pUuid, "Data".getBytes( StandardCharsets.UTF_8 ) )
                .build();

        mAdvertiseCallback = new AdvertisingSetCallback () {

            @Override
            public void onAdvertisingSetStarted(AdvertisingSet advertisingSet,
                                                int txPower,
                                                int status) {
                super.onAdvertisingSetStarted (advertisingSet, txPower, status);
            
                Toast.makeText( BluetoothActivity.this, "Started "+ status, Toast.LENGTH_LONG ).show ();

            }

            @Override
            public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
                Log.e(TAG, "Advertising onStartFailure: ");
                super.onAdvertisingSetStopped (advertisingSet);
            }
        };

        mBluetoothLeAdvertiser.startAdvertisingSet(parameters, data, null,null,null, mAdvertiseCallback);
        mTransmitTime.setText ( String.valueOf (System.currentTimeMillis ()));

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void stopTransmit() {


            enableScanBtn();


        if (mBluetoothLeAdvertiser != null) {
                mBluetoothLeAdvertiser.stopAdvertisingSet(mAdvertiseCallback);

        }
    }

Here is my scanning code:

private final ScanCallback scanCallback = new ScanCallback () {


        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onScanResult(int callbackType, ScanResult result) {

            super.onScanResult ( callbackType, result );


            if (mList.size () > 0) {
                try {
                    mList.clear ();
                    mArrayAdapter.clear ();
                    mArrayAdapter.notifyDataSetChanged ();
                    rssi = result.getRssi ();
                    id = result.getDevice ().getAddress ();

                } catch (final Exception e) {
                    e.printStackTrace ();
                }
            }

            if (result == null || result.getDevice () == null) {
                Log.e ( TAG, "-> No devices found" );
                return;
            }

            try {


                name = result.getScanRecord ().getDeviceName ();
                id = result.getDevice ().getAddress ();


                time= System.currentTimeMillis();

                if (String.valueOf ( name ) != "null"){
                    addItemToSheet ( String.valueOf(rssi), id, time );
                }


                String apDetails = "AP: " + name + "n" +
                        "UUID: " + id + "n" +
                        "RSSI: " + rssi + "n" +
                        "Time : " + time;

                Log.i ( TAG, apDetails );

                mList.add ( apDetails );


            } catch (NullPointerException e) {

                Log.e ( TAG, e.getMessage () );
            }

            mArrayAdapter = new ArrayAdapter<> ( getApplicationContext (),
                    android.R.layout.simple_list_item_1, mList );
            mListView.setAdapter ( mArrayAdapter );
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {

            super.onBatchScanResults ( results );
        }

        @Override
        public void onScanFailed(int errorCode) {

            super.onScanFailed ( errorCode );
            Toast.makeText ( BluetoothActivity.this, "onScanFailed: " + String.valueOf ( errorCode ), Toast.LENGTH_LONG ).show ();
        }

    };

Is there a way to count the number of packets in the given code?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.