Reputation: 8945
I use the API to the build an advertisement packet. I pass true
to setIncludeDeviceName
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.setIncludeTxPowerLevel(false)
.addServiceUuid(new ParcelUuid(TimeProfile.TIME_SERVICE))
.build();
The API encodes the device model number in the advertising packet. For my app however, the device name for the advertisement packet should be come from a string hard coded into the app
private static final String DEVICE_NAME = "My_Device_Name";
Is there any way to customize the device name in the advertising packet? I don't see any way to do that in the docs for AdvertiseData or for AdvertiseData.Builder
Upvotes: 2
Views: 4366
Reputation: 461
You have to remove the "setIncludeDeviceName" from your data AdvertiseData object and define a scan response AdvertiseData object
AdvertiseData scanResponse = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.build();
Then start advertising using the scanResponse as well
bluetoothAdapter.getBluetoothLeAdvertiser()
.startAdvertising(advSettings, data, scanResponse, advCallback);
Full example:
AdvertiseSettings advSettings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
.setConnectable(true)
.build();
AdvertiseData advData = new AdvertiseData.Builder()
.setIncludeTxPowerLevel(true)
.addServiceUuid(mCurrentServiceFragment.getServiceUUID())
.build();
AdvertiseData advScanResponse = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.build();
AdvertiseCallback advCallback = new AdvertiseCallback() {
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
Log.e(TAG, "Not broadcasting: " + errorCode);
int statusText;
switch (errorCode) {
case ADVERTISE_FAILED_ALREADY_STARTED:
Log.w(TAG, "ADVERTISE_FAILED_ALREADY_STARTED");
break;
case ADVERTISE_FAILED_DATA_TOO_LARGE:
Log.w(TAG, "ADVERTISE_FAILED_DATA_TOO_LARGE");
break;
case ADVERTISE_FAILED_FEATURE_UNSUPPORTED:
Log.w(TAG, "ADVERTISE_FAILED_FEATURE_UNSUPPORTED");
break;
case ADVERTISE_FAILED_INTERNAL_ERROR:
Log.w(TAG, "ADVERTISE_FAILED_INTERNAL_ERROR");
break;
case ADVERTISE_FAILED_TOO_MANY_ADVERTISERS:
Log.w(TAG, "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS");
break;
default:
Log.wtf(TAG, "Unhandled error: " + errorCode);
}
}
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
Log.v(TAG, "Advertising started");
}
};
bluetoothAdapter.getBluetoothLeAdvertiser()
.startAdvertising(advSettings, advData, advScanResponse, advCallback);
Upvotes: 2
Reputation: 8870
Pretty sure you have to use the BluetoothAdapter.setName(String name); method for this.
Upvotes: 0