Reputation: 519
We built a custom protocol for BLE communication. In this protocol we use the device name as discriminating value in communication between smartphone and beacons and use UUID as data payload.
So in my code I have:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//Needed for let the protocol work
bluetoothAdapter.setName("CUSTOMAA");
//Get Advertiser
BluetoothLeAdvertiser bluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
//Configure advertiser
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
.setConnectable(true)
.build();
//Uuid is another important part of our protocol
AdvertiseData advertiseData = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.addServiceUuid(getUuid())
.build();
//Start Advertising
bluetoothLeAdvertiser.startAdvertising(settings, advertiseData, advertiseCallback);
It works correctly but, when the App is on, if I search for the smartphone Bluetooth with another device, the name that appears is "CUSTOMAA" and not the one i set into the Bluetooth's settings page (e.g. "Marco's Android"). The original name came back if I close the App and switch off then on the Bluetooth or reboot the phone.
Is there any way to send Advertisement data with custom device name without touching the original one for other communications (e.g. a "copy" of the System BluetoothAdapter to use for Advertisement only)?
Upvotes: 4
Views: 2652
Reputation: 580
It is not the greatest solution and of course it's difficult to guarantee that it will work in all devices, but you can try the following:
AdvertiseSettings
and AdvertiseData
as it was described in the question.BluetoothAdapter
to some temp variable.BluetoothAdapter
. Wait until this name will "sink" and apply: you can do this with some while
cycle with a break condition that the adapter name has been applied: bluetoothAdapter.name == "TARGET_DEVICE_NAME_FOR_ADVERTISING"
. Of course this cycle is better to be placed somewhere outside from the main thread and have a callback for its completion.Perhaps there is a possibility of a race condition if user will change the adapter name manually during the third and fourth steps.
Upvotes: 0
Reputation: 61
I ran into the same issue, here is my idea
I used this sample project from here: https://developer.android.com/samples/BluetoothAdvertisements/index.html
in class AdvertiserService.java
, method buildAdvertiseData()
I add this line:
dataBuilder.addServiceData(Constants.Service_UUID,"my_device_name".getBytes());
this one will broadcase with service_UUID
and I add service_UUID data "my_device_name" in bytes[]
and inner class SampleScanCallback
inside ScannerFragment.java
,
method onScanResult
I can get the ScanResult with get service_UUID data
ScanResult.getScanRecord().getServiceData().get(Constants.Service_UUID)
That's my solution
Hope this help
Upvotes: 4