我想写一个应用程序在 android BLE 中做广告。我想获取“广告停止时事件”,怎么办?我需要做一些事情:当广告超时时,做一些事情。但是我可以获得超时或停止事件吗?
最佳答案
根据您的评论,这表明您想要做一些广告,然后停止,您最好的选择是使用 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);
}
}
关于android - 使用蓝牙低功耗(BLE)时如何获得广告停止事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486393/