相关文章推荐
耍酷的西红柿  ·  TN035:在 Visual C++ ...·  6 月前    · 
怕老婆的卤蛋  ·  OpenCV-android-sdk ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

i've started an advertise and tried calling stop advertiser later but the advertise just won't stop (it just keeps sending its advert even though)

advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
advSettings = new AdvertiseSettings.Builder()
        .setAdvertiseMode( AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY )
        .setTxPowerLevel( AdvertiseSettings.ADVERTISE_TX_POWER_HIGH )
        .setConnectable(false)
        .setTimeout(0)
        .build();
AdvertiseData data = new AdvertiseData.Builder()
        .setIncludeDeviceName( true )
        .setIncludeTxPowerLevel(false)
        .addServiceUuid(serviceId)
        .build();
AdvertiseData response = new AdvertiseData.Builder()
        .setIncludeDeviceName( false )
        .setIncludeTxPowerLevel(false)
        .addServiceData(serviceId, packet )
        .build();
advertiser.startAdvertising(advSettings, data, response, new AdvertiseCallback() {
    @Override
    public void onStartSuccess(AdvertiseSettings settingsInEffect) {
        super.onStartSuccess(settingsInEffect);
        Log.d(TAG, "Packet was successfully sent");
    @Override
    public void onStartFailure(int errorCode) {
        super.onStartFailure(errorCode);
        Log.e(TAG, "Advertise failed : " + errorCode);

stopAdvertising Method :

public void stopAdvertising(){
        Log.d(TAG, "stoping advertising ..." );
        advertiser.stopAdvertising(new AdvertiseCallback() {
            @Override
            public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                Log.d(TAG, "onStopSuccess" );
                super.onStartSuccess(settingsInEffect);
            @Override
            public void onStartFailure(int errorCode) {
                Log.e(TAG, "onStopFailed" + errorCode );
                super.onStartFailure(errorCode);
        advertiser = null;

But when i call the stopAdvertising method the advertising doesn't stop, i don't no if it can help but, when i call my method stopAdvertising i get :

D/ContentValues: stoping advertising ...
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothLeAdvertiser: wrapper is null

Answer to my own question :

The problem was that the callback for stopAdvertising and startAdvertising must be the same object as said by google doc :

* Stop Bluetooth LE advertising. The {@code callback} must be the same one use in
* {@link BluetoothLeAdvertiser#startAdvertising}.

(the callback is used to identify the instance of advertise by google's sdk)

Using the same callback for both worked for me

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.