在Android上,我可以注册一个回调来告诉我蓝牙是打开还是关闭?

树木无处不在

我需要在我的应用程序内知道蓝牙是打开还是关闭。或者,例如从OS设置下拉菜单中打开或关闭了蓝牙。我以为可以在“活动”中执行此操作onResume()但是事实证明,打开Android OS的设置“下拉菜单”(通过从屏幕顶部边缘用手指下拉来访问该菜单)时,活动不会暂停。

蓝牙可用或不可用时,我需要更新UI。

我可以注册一个回调(例如BroadcastReceiver)或任何其他回调以让系统在蓝牙可用性发生变化时告诉我吗?

马蒂亚·梅斯特里尼(Mattia Maestrini)

您可以使用意图过滤器注册接收器:

<receiver
        android:name="com.example.BluetoothReceiver"
        android:enabled="true">
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
    </intent-filter>
</receiver>

这是BroadcastReceiver:

public class BluetoothReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

            switch (state){
                case BluetoothAdapter.STATE_OFF:
                    //Indicates the local Bluetooth adapter is off.
                break;

                case BluetoothAdapter.STATE_TURNING_ON:
                    //Indicates the local Bluetooth adapter is turning on. However local clients should wait for STATE_ON before attempting to use the adapter.
                break;

                case BluetoothAdapter.STATE_ON:
                    //Indicates the local Bluetooth adapter is on, and ready for use.
                break;

                case BluetoothAdapter.STATE_TURNING_OFF:
                    //Indicates the local Bluetooth adapter is turning off. Local clients should immediately attempt graceful disconnection of any remote links.
                break;
            }
        }
    }
};

或者,如果您想直接在活动中添加:

public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        //Indicates the local Bluetooth adapter is off.
                        break;

                    case BluetoothAdapter.STATE_TURNING_ON:
                        //Indicates the local Bluetooth adapter is turning on. However local clients should wait for STATE_ON before attempting to use the adapter.
                        break;

                    case BluetoothAdapter.STATE_ON:
                        //Indicates the local Bluetooth adapter is on, and ready for use.
                        break;

                    case BluetoothAdapter.STATE_TURNING_OFF:
                        //Indicates the local Bluetooth adapter is turning off. Local clients should immediately attempt graceful disconnection of any remote links.
                        break;
                }
            }
        }
    };
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

nmap告诉我一个服务是“打开的”,而本地是“打开的”,如何打开?

我可以编写一个keras回调来记录并返回总训练时间吗?

我可以让git告诉我一个用户修改过的所有文件吗?

为什么Word VBA会告诉我一个对话框已打开?

有人可以告诉我一个简单的PagerSlidingTabStrip工作实现吗?

有人可以告诉我最后一个循环在做什么吗?

它告诉我在 php 上使用 <td style="color:...;"> 时犯了一个错误

谁能告诉我为什么媒体查询是在断点之前注册而不是一个即时定位

我在一个朋友的地方,在他的wifi上。他能告诉我访问哪些网站吗?

有人可以告诉我一个好的依赖矩阵是什么样子并指定原因吗?

请告诉我为什么我的输出中有一个“none”

为什么我不告诉我,Python Threading会运行一个函数?

我想创建一个脚本,告诉我何时发布帖子

我实现了同样的功能有不同的方法,任何人可以告诉我为什么我的最后一个函数不打印假的?

FXML 一直告诉我有一个空字符串

我已经使用JOptionPane.showOptionDialog显示了一个JDialog,有人可以告诉我如何将其设置为inVisible或Dispose吗?

使一个xmlhttprequest等待告诉我从处理程序得到响应?

在数组中查找元素告诉我参数必须是一个对象

Linux告诉我正在使用一个串行端口,但是没有使用

pycharm告诉我我安装了一个软件包,但是我不能使用该pakege

VS Code中的Python告诉我一个数字小于一个较小的数字

如何创建一个列来告诉我一个数字出现的次数?

在一个Perl脚本中,我可以关闭然后重新打开STDIN吗?

麻烦制作一个可以告诉我 C 中两个数的商的分数和小数形式的计算器

有人可以告诉我为什么这不是一个常数表达式吗?

它仅显示列表的第一个元素。有人可以告诉我错误是什么吗?

对于@SpringBootTest,为什么我没有一个告诉我“配置错误:发现@BootstrapWith的多个声明”?

一个网站告诉我我的密码被盗,这是否意味着该网站也不安全?

为什么C告诉我我需要一个指向MSP430函数的指针