Search code examples
androidbluetoothadshololens

Stop advertising after Server received message


I'm trying to send a simple string from android device to hololens via bluetooth.

I already completed this, but the android device continuously send the string to the server, after calling startAdvertising();

BluetoothAdapter mBluetoothAdapter;
BluetoothLeAdvertiser mBLEAdvertiser;

private void startAdvertising(int manufactureId, byte[] manufactureData) throws InterruptedException {

    if (mBLEAdvertiser == null) return;
    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
            .setConnectable(false)
            .setTimeout(0)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
            .build();
    AdvertiseData data = new AdvertiseData.Builder()
            .addManufacturerData(manufactureId, manufactureData)
            .build();
        mBLEAdvertiser.startAdvertising(settings, data, mAdvertiseCallback);

Solution

  • When AdvertiseSettings.Builder#setTimeout() sets the value of the advertising time to 0, the time limit will be disabled and the advertising is always work unless BluetoothLeAdvertiser#stopAdvertising() is called.

    More information please see: https://developer.android.com/reference/android/bluetooth/le/AdvertiseSettings.Builder.html#setTimeout(int)