简体   繁体   中英

Is there any way to change the device name in AdvertiseData in android BLE?

    AdvertiseData advertiseData = new AdvertiseData.Builder()
            .setIncludeDeviceName( true )
            .addServiceUuid( uuid )
            .build();

When creating a AdvertiseData object, it will fail if you add both your device name and service uuid in your packet because it is bigger than the maximum size of the AdvertiseData limits. Is there any way to make it possible to contain both device name and service uuid in a advertise data? I know iOS can do this. Can android do it?

Or, is there any way to change the displayed device name? Like how to set the device name in advertise data?

The device name is stored in the BluetoothAdapter. You can set it using the following:

boolean isNameChanged = BluetoothAdapter.getDefaultAdapter().setName("myDeviceName");

One way is to change the device name to a shorter one (that does not make the packet exceed 31 bytes), as mentioned in the answer above. For example "Pixel 3" is fine, but "Xperia Premium" is too long. Check https://stackoverflow.com/a/57759036/11194589 for reference.

Another solution is to remove the device name from the main advertisement packet and put it in scan response as mentioned in https://stackoverflow.com/a/49631117/11194589 .

Something like this in kotlin (advertisementCallback not included):

    val bluetoothLeAdvertiser: BluetoothLeAdvertiser? =
            bluetoothManager.adapter.bluetoothLeAdvertiser

    bluetoothLeAdvertiser?.let {bluetoothAdvertiser ->
        val settings = AdvertiseSettings.Builder()
                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
                .setConnectable(true)
                .setTimeout(0)
                .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
                .build()

        val data = AdvertiseData.Builder()
                .setIncludeTxPowerLevel(false)
                .addServiceUuid(ParcelUuid(MY_SERVICE_UUID))
                .build()

        val scanResponse = AdvertiseData.Builder()
                .setIncludeDeviceName(true)
                .build()

        bluetoothAdvertiser.startAdvertising(settings, data, scanResponse, advertisementCallback)

    } ?: Timber.w("Failed to create advertiser")

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