1

I am working on an application in which I want to set my Bluetooth Adapter device name after receiving response from server in background and starts advertising. So the flow is like

I call startAsPeripheral Function in my onStartCommand first time and it's start advertising perfectly but when I receive data from server I need to stop my running advertisement, set my bluetooth adapter new name then starts advertising again.

So the issue is whenever I scan my advertising packet using other apps like nRF or even simply after opening my bluetooth scan page from settings of mobile it showings me device multiple times with different names which i have already set.

So my StartAsPeripheral function is

 private void startAsPeripheral() {
    stopAdvertising();
    stopServer();
    mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    mBluetoothAdapter = mBluetoothManager.getAdapter();
    mBluetoothAdapter.getDefaultAdapter().setName(Constant.userInfo.getUserCode() + " " + 
  Constant.userInfo.getHealthScore());

    if (!mBluetoothAdapter.getName().contains(Constant.userInfo.getUserCode())) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mBluetoothAdapter.getDefaultAdapter().setName(Constant.userInfo.getUserCode() + " " + 
 Constant.userInfo.getHealthScore());
    }

    mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
    Log.d("BluetoothCheck", mBluetoothAdapter.getName() + " After setting device name");

    GattServerCallback gattServerCallback = new GattServerCallback();
    mGattServer = mBluetoothManager.openGattServer(this, gattServerCallback);
    setupServer(mGattServer);

    startAdvertising(mBluetoothLeAdvertiser);
}

and StartAdvertising is

 private void startAdvertising(BluetoothLeAdvertiser mBluetoothLeAdvertiser) {
    if (mBluetoothLeAdvertiser == null) {
        return;
    }
    ParcelUuid parcelUuid = new ParcelUuid(SERVICE_UUID);
    AdvertiseSettings advSettings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
            .setConnectable(true)
            .build();

    AdvertiseData advData = new AdvertiseData.Builder()
            .setIncludeTxPowerLevel(true)
            .addServiceUuid(parcelUuid)
            .build();

    AdvertiseData advScanResponse = new AdvertiseData.Builder()
            .setIncludeDeviceName(true)
            .build();

    advCallback = new AdvertiseCallback() {
        @Override
        public void onStartFailure(int errorCode) {
            super.onStartFailure(errorCode);
            Log.e(TagName, "Not broadcasting: " + errorCode);
            int statusText;
            switch (errorCode) {
                case ADVERTISE_FAILED_ALREADY_STARTED:
                    Log.w(TagName, "ADVERTISE_FAILED_ALREADY_STARTED");
                    break;
                case ADVERTISE_FAILED_DATA_TOO_LARGE:
                    Log.w(TagName, "ADVERTISE_FAILED_DATA_TOO_LARGE");
                    break;
                case ADVERTISE_FAILED_FEATURE_UNSUPPORTED:
                    Log.w(TagName, "ADVERTISE_FAILED_FEATURE_UNSUPPORTED");
                    break;
                case ADVERTISE_FAILED_INTERNAL_ERROR:
                    Log.w(TagName, "ADVERTISE_FAILED_INTERNAL_ERROR");
                    break;
                case ADVERTISE_FAILED_TOO_MANY_ADVERTISERS:
                    Log.w(TagName, "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS");
                    break;
                default:
                    Log.wtf(TagName, "Unhandled error: " + errorCode);
            }
        }

        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            super.onStartSuccess(settingsInEffect);
            Log.v(TagName, "Advertising started");
            Log.d("BluetoothCheck", mBluetoothLeAdvertiser.toString() + " Advertise Callback");
        }

    };
    mBluetoothLeAdvertiser.startAdvertising(advSettings, advData, advScanResponse, advCallback);
    Log.d("BluetoothCheck", mBluetoothLeAdvertiser.toString() + " Starting Advertising");
}

private void stopServer() {
    if (mGattServer != null) {
        mGattServer.close();
        Log.d("BluetoothCheck", mBluetoothLeAdvertiser.toString() + " Stop Gatt Server");
    }
}

private void stopAdvertising() {
    if (mBluetoothLeAdvertiser != null) {
        mBluetoothLeAdvertiser.stopAdvertising(advCallback);
        Log.d("BluetoothCheck", mBluetoothLeAdvertiser.toString() + " Stop Advertising");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } 
}

So all I want to show one advertisement at a time so the scanning will receive only advertisement. I am using BLE for it. So now you have code and you guys can let me know what I am doing wrong ?

Khurram Ansar
  • 97
  • 1
  • 10

0 Answers0