android 检查蓝牙是否打开/关闭

Mic*_*rik 1 android bluetooth

我做了一个短信接收者的文字来说话,他们只有在蓝牙打开并在广播中连接时才会说话。我在谷歌上查看,找到的教程告诉我如何连接,但我找不到合适的来检查是打开还是关闭。我开始

if (bluetooth == on){
//do some stuff.
}
else {
// do other stuff
Run Code Online (Sandbox Code Playgroud)

这是我的代码,如果它可以帮助您理解:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;


 public class RecevoirSms extends BroadcastReceiver
{
final SmsManager sms = SmsManager.getDefault();

@Override
public void onReceive(Context context, Intent intent) 
{
    final Bundle bundle = intent.getExtras();          
    final Object[] pdusObj = (Object[]) bundle.get("pdus");         
    for (int i = 0; i < pdusObj.length; i++) {                      
    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);     
    String phoneNumber = currentMessage.getDisplayOriginatingAddress();            
    String senderNum = phoneNumber;             
    String Message = currentMessage.getDisplayMessageBody();   

 if (senderNum.contains("3330") || senderNum.contains("5149921188") || senderNum.contains("9000"))   {
}

 else {

    String word = senderNum;
    String remove ="+1";
    String Numero = (removeWords(word, remove));
    MemoireCourtTerme.Nsms =  Numero;  
    MemoireCourtTerme.Message = Message;

    if (Message.contains("eva") && Message.contains("?") || Message.contains("Eva") && Message.contains("?")){
       Intent i2 = new Intent(context,EvaSms.class); 
       i2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(i2); 
    }

    else {
           MemoireCourtTerme.Mode = "SmsRecu";
           MemoireCourtTerme.ReponceEva = "Vous avez reçu un nouveau message voulez vous l'écoutez?";
           MemoireCourtTerme.ReponceEvaDuree = "5000";
           Intent i2 = new Intent(context,Parole.class); 
           i2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
           context.startActivity(i2); 
        }


            }                         
}
}
public static String removeWords(String word, String remove){
    return word.replace(remove, "");
}
}
Run Code Online (Sandbox Code Playgroud)

Spr*_*ies 5

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    // Device does not support Bluetooth
} else {
    if (!mBluetoothAdapter.isEnabled()) {
        // Bluetooth is not enable :)
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用权限:-

 <uses-permission android:name="android.permission.BLUETOOTH" />
Run Code Online (Sandbox Code Playgroud)

用于耳机配置文件控制

BluetoothHeadset mBluetoothHeadset;

// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);

private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        if (profile == BluetoothProfile.HEADSET) {
            mBluetoothHeadset = (BluetoothHeadset) proxy;
        }
    }
    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.HEADSET) {
            mBluetoothHeadset = null;
        }
    }
};

// ... call functions on mBluetoothHeadset

// Close proxy connection after use.
mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);
Run Code Online (Sandbox Code Playgroud)