Marco Vasapollo
Marco Vasapollo

Reputation: 519

BLE Advertising in Android with custom BT Adapter Name without touching the System one

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

Answers (2)

Andrei K.
Andrei K.

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:

  1. Prepare required AdvertiseSettings and AdvertiseData as it was described in the question.
  2. Save the current name of the BluetoothAdapter to some temp variable.
  3. Set the target advertising name for the 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.
  4. When the target device name has been applied, start the advertising as it was mentioned in the question.
  5. Restore the adapter name which was saved on the second step in the way described in the third step.

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

hungnv39
hungnv39

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

Related Questions