Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android检查蓝牙是否开启/关闭_Android_Bluetooth - Fatal编程技术网

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

android检查蓝牙是否开启/关闭,android,bluetooth,Android,Bluetooth,我给短信接收者发了一条短信给speach,他们只在蓝牙开启并在boardcast中连接的情况下才说话。我在谷歌上搜索,找到的教程告诉我如何连接,但我找不到正确的方法来检查它是开还是关。我从 if (bluetooth == on){ //do some stuff. } else { // do other stuff 这是我的代码,如果它可以帮助您理解: import android.content.BroadcastReceiver; import android.content.Cont

我给短信接收者发了一条短信给speach,他们只在蓝牙开启并在boardcast中连接的情况下才说话。我在谷歌上搜索,找到的教程告诉我如何连接,但我找不到正确的方法来检查它是开还是关。我从

if (bluetooth == on){
//do some stuff.
}
else {
// do other stuff
这是我的代码,如果它可以帮助您理解:

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, "");
}
}
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.smsmsmanager;
导入android.telephony.sms消息;
公共类receivoirSMS扩展了BroadcastReceiver
{
final smsmsmanager sms=smsmsmanager.getDefault();
@凌驾
公共void onReceive(上下文、意图)
{
final Bundle=intent.getExtras();
最终对象[]pdusObj=(对象[])bundle.get(“pdus”);
对于(int i=0;i
和使用权限:-

 <uses-permission android:name="android.permission.BLUETOOTH" />
和使用权限:-

 <uses-permission android:name="android.permission.BLUETOOTH" />

然后会出现一个警报,允许用户响应请求。有一个
BluetoothAdapter
函数
enable()
,但明确禁止在特定情况下使用它

BluetoothAdapter BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(getApplication(),"Bluetooth not available",Toast.LENGHT_LONG).show();
}
else{  
if (BluetoothAdapter.isEnabled()) {
Toast.makeText(getApplication(),"Bluetooth is ON",Toast.LENGHT_LONG).show();
}
else{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}}

然后会出现一个警报,允许用户响应请求。有一个
BluetoothAdapter
函数
enable()
,但明确禁止在特定情况下使用它

BluetoothAdapter BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(getApplication(),"Bluetooth not available",Toast.LENGHT_LONG).show();
}
else{  
if (BluetoothAdapter.isEnabled()) {
Toast.makeText(getApplication(),"Bluetooth is ON",Toast.LENGHT_LONG).show();
}
else{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}}
使用此代码:

AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
boolean retval = mAudioManager.isBluetoothScoOn();
使用此代码:

AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
boolean retval = mAudioManager.isBluetoothScoOn();
那样

val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
 registerReceiver(bluetoothChangeReceiver, filter)


 private val bluetoothChangeReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            val action = intent.action

            if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
               val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR)
                when (state) {
                    BluetoothAdapter.STATE_OFF -> Log.i(TAG, "Bluetooth off")
                    BluetoothAdapter.STATE_TURNING_OFF -> Log.i(TAG, "Turning Bluetooth off...")
                    BluetoothAdapter.STATE_ON -> Log.i(TAG, "Bluetooth on")
                    BluetoothAdapter.STATE_TURNING_ON -> Log.i(TAG, "Turning Bluetooth on...")
                }
            }
        }
    }
那样

val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
 registerReceiver(bluetoothChangeReceiver, filter)


 private val bluetoothChangeReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            val action = intent.action

            if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
               val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR)
                when (state) {
                    BluetoothAdapter.STATE_OFF -> Log.i(TAG, "Bluetooth off")
                    BluetoothAdapter.STATE_TURNING_OFF -> Log.i(TAG, "Turning Bluetooth off...")
                    BluetoothAdapter.STATE_ON -> Log.i(TAG, "Bluetooth on")
                    BluetoothAdapter.STATE_TURNING_ON -> Log.i(TAG, "Turning Bluetooth on...")
                }
            }
        }
    }

此问题的可能重复项已被回答,请参阅此答案否我需要检查蓝牙适配器是否已连接此问题的可能重复项已被回答,请参阅此答案否我需要检查蓝牙适配器是否已连接此代码有效,但仅当蓝牙模式打开时有效。我需要知道蓝牙耳机是什么connect@MichelSurik您想检查耳机是否已连接?是的,使用耳机讲话的文字很好,但没有耳机则不行usefull@MichelSurik您能告诉我您到底想要什么吗?我想检查蓝牙适配器是否已连接且已打开,因为文本中有“如果”字样如果我的短信处于关闭状态,则此代码仅在蓝牙模式打开时有效。我需要知道蓝牙耳机是什么connect@MichelSurik您想检查耳机是否已连接?是的,使用耳机讲话的文字很好,但没有耳机则不行usefull@MichelSurik您能告诉我您到底想要什么吗?我想检查蓝牙适配器是否已连接且已打开,因为文本中有“如果”字样我的短信如果是关闭的,没有别的事了