Android启用/禁用蓝牙

时间:2017-03-30 15:26:59

标签: android bluetooth

我的应用程序中有一个代码,按下按钮可以打开和关闭蓝牙。

当蓝牙打开或关闭时,我想将此按钮的背景更改为绿色和红色。我为它做了google答案,并且有一个类似于它的stackoverflow帖子,他提到的方式不起作用。我注册了一个接收器,像往常一样有一个开关盒,我提到这个按钮的颜色变化,它不起作用。

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

该守则不是关于如何影响颜色,而是从广播接收器访问蓝牙的状态变化

   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;
                }
            }

        }
    };

1 个答案:

答案 0 :(得分:0)

您需要在清单中声明BroadcastReceiver以收听蓝牙状态。本文总结得很好: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>