2014-09-05 2 views
1

나는 블루투스가 켜져 있고 게시판에 연결되어있을 때만 말하기 위해 SMS 수신자 텍스트를 작성했습니다. 나는 구글을 찾고 튜토리얼은 연결하는 법을 알려주지 만 켜져 있는지 여부를 확인하는 올바른 방법을 찾지 못한다. 헤드셋 프로파일 제어를 들어블루투스가 켜져 있는지 안드로이드 확인

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

- :

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) –

+0

이 질문은 이미 답변되었습니다.이 대답을 참조하십시오. http://stackoverflow.com/a/11242618/1225413 –

+0

아니오 블루투스 어댑터가 연결되어 있는지 확인해야합니다. –

답변

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

및 사용 권한을 : 나는 당신이 이해하는 데 도움이 될 수 있으면이 내 코드입니다

if (bluetooth == on){ 
//do some stuff. 
} 
else { 
// do other stuff 

시작

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

블루투스 모드가 켜져있는 경우에만이 코드가 작동합니다. 블루투스 헤드셋에 대해 알아야합니다 –

+0

@MichelSurik 유 날씨 헤드셋이 연결되어 있는지 확인하고 싶습니까? –

+0

예 헤드셋으로 발언 할 텍스트가 유용하지만 유용하지 않습니다. –

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); 
}} 
[블루투스는 안드로이드 응용 프로그램을 사용하여 활성화되어 있는지 확인합니다]의
+0

미안 해요 –

관련 문제