使用蓝牙低功耗(BLE)时如何获得广告停止事件?

How to get the advertising stop event when using bluetooth low energy (BLE)?

我想写一个应用程序在 android BLE 中做广告。我想得到"when advertising stop event",我该怎么办?我需要做一些事情:当广告超时时,做一些事情。但是我可以获得超时或停止事件吗?

根据你的评论,这表明你想做点广告,然后停止,你最好的选择是使用 Handler

Runnable() 代码中,您可以设置一个标志,就像我在下面所做的那样,或者调用一些其他方法来处理更复杂的操作。如果您在超时到期之前获得连接的设备,您可以调用 mHandler.removeCallBacks().

import android.os.Handler;

public class thisClase {
    Handler mHandler = new Handler();
    boolean timedOut = false;
    public static final int TIMEOUT = 30*1000; //30 second timeout

    Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            //Put your code to stop advertisting here.
            timedOut = true;
        }
    }

    public void startAdvert() {
        mHandler.postDelayed(mRunnable, TIMEOUT);

        //Put code to start advertising here.
        timedOut=false;
        //Start Advertising.
    }

    //Call this method when a device connects to cancel the timeout.
    public void whenDeviceConnects() {
        //Cancel the runnable.
        mHandler.removeCallbacks(mRunnable);
    }
}