安卓 禁用 蓝牙_Android启用/禁用蓝牙

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

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.

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;

}

}

}

};

解决方案

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)

android:name="com.myapp.BluetoothReceiver"

android:enabled="true"

android:exported="true">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值