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

标签 android bluetooth-lowenergy

我想写一个应用程序在 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/

相关文章:

Android 6.0 Marshmallow BLE 连接问题

android - 处理程序后执行队列的顺序不正确

Android BLE 连接成功后有一些延迟

android - 从表中选择 2 列,在一个查询

java - 自定义布局中 switchpreference 中开/关切换的颜色变化

android - Android 9 上的生物识别管理器

ubuntu - Hcitool 白名单命令不起作用

具有不同屏幕尺寸的Android绝对布局

Android Studio - 使用依赖于另一个 Android 库的 Android 库构建 Android 项目

ios - GetDescriptor() 空值/128 位 UUID 描述符?