我制作了一个sms接收器文本,只有在蓝牙打开并且在电路板连接时它们才能通话。我看谷歌和找到的教程告诉我如何连接,但我找不到合适的一个来检查是否打开或关闭。我从
开始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 :(得分: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" />
适用于HeadSet配置文件控制
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);
答案 1 :(得分: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);
}}
答案 2 :(得分:0)
使用此代码:
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
boolean retval = mAudioManager.isBluetoothScoOn();
答案 3 :(得分:0)
喜欢
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...")
}
}
}
}