简体   繁体   中英

BLE advertisement with different data packets in android

I am new in Bluetooth low energy advertisement. BLE supports dynamic advertisement from API level 28. as you can advertise different data packets. I am trying to do but getting status code 18 and Null in Advertising set. Can anyone please tell me how can I do dynamic advertisement in android. My device is Nexus 6 with Android 8.1.0.

here is my code :

 //setting parameters

AdvertisingSetParameters parameters = (new AdvertisingSetParameters.Builder())
            .setLegacyMode(false)
            .setConnectable(true)
            .setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
            .setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
            .build();

//Adding advertising data

AdvertiseData.Builder mbuilder = new AdvertiseData.Builder();
    mbuilder.setIncludeDeviceName(false);
    mbuilder.setIncludeTxPowerLevel(false);

mbuilder.addManufacturerData(Integer.parseInt(manuf, 16), manufacturerData1);
    AdvertiseData mData = mbuilder.build();

//Adding scan response data

AdvertiseData scanResponse = new AdvertiseData.Builder()
            .setIncludeDeviceName(true)
            .build();

//Advertising set call back and I am receiving null in Advertising set and status code 18

AdvertisingSetCallback callback = new AdvertisingSetCallback() {
        @Override
        public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
            Log.i("", "onAdvertisingSetStarted(): txPower:" + txPower + " , status: "
                    + status);

            if (status==AdvertisingSetCallback.ADVERTISE_FAILED_ALREADY_STARTED)
                Toast.makeText(context, "ADVERTISE_FAILED_ALREADY_STARTED", Toast.LENGTH_SHORT).show();
            else if (status==AdvertisingSetCallback.ADVERTISE_FAILED_FEATURE_UNSUPPORTED)
                Toast.makeText(context, "ADVERTISE_FAILED_FEATURE_UNSUPPORTED", Toast.LENGTH_SHORT).show();
            else if (status==AdvertisingSetCallback.ADVERTISE_FAILED_DATA_TOO_LARGE)
                Toast.makeText(context, "ADVERTISE_FAILED_DATA_TOO_LARGE", Toast.LENGTH_SHORT).show();
            else if (status==AdvertisingSetCallback.ADVERTISE_FAILED_INTERNAL_ERROR)
                Toast.makeText(context, "ADVERTISE_FAILED_INTERNAL_ERROR", Toast.LENGTH_SHORT).show();
            else if (status==AdvertisingSetCallback.ADVERTISE_FAILED_TOO_MANY_ADVERTISERS)
                Toast.makeText(context, "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS", Toast.LENGTH_SHORT).show();
            else if (status==AdvertisingSetCallback.ADVERTISE_SUCCESS)
                Toast.makeText(context, "ADVERTISE_SUCCESS", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
            Log.i("", "onAdvertisingSetStopped():");
        }
    };

//starting advertisement

    mBluetoothLeAdvertiser.startAdvertisingSet(parameters, mData, scanResponse, null, null, callback);

Do you really need "scanResponse"? My device has BLE 5.0 and Android 9.0. If I pass "null" instead "scanResponse", then the current object and the correct status are returned to the callback function. But if I pass "scanResponse", then gets "null" object and "status" 18.

If change

mBluetoothLeAdvertiser.startAdvertisingSet(parameters, mData, scanResponse, null, null, callback);

on

mBluetoothLeAdvertiser.startAdvertisingSet(parameters, mData, null, null, null, callback);

all work for me and I can scanned BLE 5 signal from my smartphone on other devices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM