Android启用/禁用蓝牙 [英] Android Enable/ Disable Bluetooth

查看:756
本文介绍了Android启用/禁用蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中有一个代码,只需按一下按钮即可打开和关闭蓝牙.

I have a code, in my app, where a button press turn on bluetooth on and off.

当蓝牙处于打开或关闭状态时,我想将此按钮的背景更改为绿色和红色.我做了谷歌的答案,有一个类似于它的stackoverflow帖子,他提到的方式是行不通的.我注册了一个接收器,并像往常一样有一个开关盒,其中提到了此按钮颜色的更改,但它不起作用.

I want to change the background of this button to green and red, when the bluetooth is either on or off. I did google answers for it and there is a stackoverflow post similar to it and the way he mentioned is not working. I registered a receiver and have a switch case as usual, where I mentioned this button color change and it s not working.

事实上,接收器中的log.d甚至没有显示在android studio的终端中.

Infact the log.d in the receivers are not even being shown up in the terminal of android studio.

该规范并不涉及如何实现色彩,而是处理来自广播接收器的蓝牙状态更改

THE CODE IS NOT ABOUT HOW TO IMPLETMENT THE COLOR, BUT ACCESSING THE STATE CHANGE OF BLUETOOTH FROM BROADCAST RECEIVER

   public void enableDisableBT(){
        if(mBluetoothAdapter == null) {
            Log.d(TAG, "enableDisableBT: Does not have BT capabilities");
            Toast.makeText(getApplicationContext(),"No Bluetooth Capability", Toast.LENGTH_SHORT).show();
        }
        if (!mBluetoothAdapter.isEnabled()){
            Log.d(TAG, "enableDisableBT: enabling BT");
            //btnONOFF.setBackgroundColor(Color.GREEN); // Since bluetooth is NOT enabled, it enables bluetotoh and sets background color to green
            Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableBTIntent);

            IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
            registerReceiver(mBroadcastReceiver1, BTIntent);
        }
        if(mBluetoothAdapter.isEnabled()){
            Log.d(TAG, "enableDisableBT: disabling BT");
            mBluetoothAdapter.disable();
            btnONOFF.setBackgroundColor(Color.RED);// Since bluetooth is  enabled, it disables bluetotoh and sets background color to green
            incomingMessages.setText("Incoming Messages");
            IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
            registerReceiver(mBroadcastReceiver1, BTIntent);
        }

    }

 private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // when discovery finds a device
            if (action.equals(mBluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)){
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,mBluetoothAdapter.ERROR);

                switch (state){
                    case BluetoothAdapter.STATE_OFF:
                        Log.d(TAG, "onReceive: STATE OFF");
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        Log.d(TAG, "mBroadcastReceiver1: STATE TURNING OFF");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        //Log.d(TAG, "mBroadcastReceiver1: STATE ON");
                        btnONOFF.setBackgroundColor(Color.GREEN);
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON:
                        Log.d(TAG, "mBroadcastReceiver1: STATE TURNING ON");
                        break;
                }
            }

        }
    };

推荐答案

您需要在清单中声明一个BroadcastReceiver,以侦听蓝牙状态.本文对此进行了很好的总结: https://droidhat.com/broadcast接收器使用蓝牙状态更改

You need to declare a BroadcastReceiver in your manifest to listen to Bluetooth state. This article sums it up well: https://droidhat.com/broadcast-receiver-using-change-in-bluetooth-status

1)

/**
 * A receiver that listens to Bluetooth getting turned on.
 */
 public class BluetoothReceiver extends BroadcastReceiver {
   @Override public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    int state;
    switch (action) {
        case BluetoothAdapter.ACTION_STATE_CHANGED:
            state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
            if (state == BluetoothAdapter.STATE_ON) {
                // change my button color
            }
     }
   } 
 }

2)

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

这篇关于Android启用/禁用蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆