2014-09-05 205 views
1

我发短信接收文本进行演讲,他们只在蓝牙开启和连接时进行通话。我看谷歌和教程发现告诉我如何连接,但我找不到合适的检查是否打开或关闭。我开始与android检查蓝牙是否开启/关闭

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, ""); 
} 
} 
+0

可能重复(http://stackoverflow.com/questions/11242519/check-if-bluetooth-is-enabled-using-an-android-application ) – 2014-09-05 11:35:43

+0

这个问题已经被回答,参考这个答案http://stackoverflow.com/a/11242618/1225413 – 2014-09-05 11:36:54

+0

不,我需要检查,如果蓝牙适配器连接 – 2014-09-08 00:07:13

回答

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

和使用权限: -

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

对于耳机模式控制

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); 
+0

此代码的工作,但只有当蓝牙模式打开。我需要知道蓝牙耳机是连接 – 2014-09-07 18:23:35

+0

@MichelSurik你想检查天气耳机连接与否? – 2014-09-08 04:23:50

+0

是的文字与耳机说话这是好的,但没有不是有用的 – 2014-09-08 12:59:38

0

然后会出现一个警报,允许用户响应请求。有一个BluetoothAdapter功能enable(),但documentation明确不鼓励使用它,除了在特定情况下。

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); 
}} 
的[检查蓝牙是使用Android应用程序启用]
+0

不工作抱歉 – 2014-09-07 18:20:01