Reputation: 438
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?
Upvotes: 3
Views: 7966
Reputation: 159
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")
Upvotes: 3
Reputation: 114
The device name is stored in the BluetoothAdapter. You can set it using the following:
boolean isNameChanged = BluetoothAdapter.getDefaultAdapter().setName("myDeviceName");
Upvotes: 7