安卓监听蓝牙电话状态
2023-07-05 本文已影响0人
Ad大成
package com.tencent.jsdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.lang.reflect.Method;
public class PhoneStateReceiver extends BroadcastReceiver {
private static final String TAG = "PhoneStateReceiver";
private int lastState = -1;
public static final String BT_HEADSET_CLIENT_EXTRA_CALL = "android.bluetooth.headsetclient.extra.CALL";
public static final String BT_HEADSET_CLIENT_ACTION_CALL_CHANGED = "android.bluetooth.headsetclient.profile.action.AG_CALL_CHANGED";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// Log.e(TAG"PhoneStateReceiver PhoneStateReceiver action: " + action);
Log.e(TAG, "onReceive: ==============>" );
if (BT_HEADSET_CLIENT_ACTION_CALL_CHANGED.equals(action)){
Object bluetoothHeadsetClientCall = intent.getParcelableExtra(BT_HEADSET_CLIENT_EXTRA_CALL);//注:getExtra()返回Object类型
String stateStr = String.valueOf(bluetoothHeadsetClientCall);
Log.e(TAG,"PhoneStateReceiver bluetoothHeadsetClientCall = " + stateStr);
try {
Class clazz = Class.forName("android.bluetooth.BluetoothHeadsetClientCall");
Method method = clazz.getMethod("getState");
int state = (int)method.invoke(bluetoothHeadsetClientCall);
// if (state != Utils.CALL_STATE_TERMINATED && state != lastState){
//// GameManager.getInstance().gamePause();
// }
lastState = state;
Log.e(TAG,"PhoneStateReceiver state = " + state);
} catch (Exception e) {
e.printStackTrace();
}
// BluetoothHeadsetClientCall ss = (BluetoothHeadsetClientCall)state;//类型转换
// LogUtils.e(TAG,"ss.mId = " + String.valueOf(ss.getId()));
// /*state:: CALL_STATE_ACTIVE =0; CALL_STATE_HELD = 1; CALL_STATE_DIALING =2*/
// LogUtils.e(TAG,"ss.mState = " + String.valueOf(ss.getState()));//获取来电/去电/接通/挂断等状态
// LogUtils.e(TAG,"ss.mNumber = " + String.valueOf(ss.getNumber()));//获取来电/去电的电话号码
// LogUtils.e(TAG," ss.mOutgoing = " + String.valueOf(ss.isOutgoing()));
// LogUtils.e(TAG,"ss.mMultiParty = " + String.valueOf(ss.isMultiParty()));
}else if (Intent.ACTION_NEW_OUTGOING_CALL.equals(action)) {
// 去电,可以用定时挂断
// 双卡的手机可能不走这个Action
//String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//LogUtils.d(TAG, "PhoneStateReceiver EXTRA_PHONE_NUMBER: " + phoneNumber);
// GameManager.getInstance().gamePause();
} else {
// 来电去电都会走
// 获取当前电话状态
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Log.e(TAG,"PhoneStateReceiver onReceive state: " + state);
// 获取电话号码
//String extraIncomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
//LogUtils.d(TAG, "PhoneStateReceiver onReceive extraIncomingNumber: " + extraIncomingNumber);
if (TelephonyManager.EXTRA_STATE_RINGING.equalsIgnoreCase(state) || TelephonyManager.EXTRA_STATE_OFFHOOK.equalsIgnoreCase(state)) {
// GameManager.getInstance().gamePause();
}
}
}
}
private void testBluetooth() {
PhoneStateReceiver phoneStateReceiver = new PhoneStateReceiver();
IntentFilter filter_dynamic = new IntentFilter();
filter_dynamic.addAction("android.intent.action.PHONE_STATE");
filter_dynamic.addAction("android.intent.action.NEW_OUTGOING_CALL");
filter_dynamic.addAction("android.bluetooth.headsetclient.profile.action.AG_CALL_CHANGED");//BluetoothHeadsetClient.ACTION_CALL_CHANGED);
Log.e("PhoneStateReceiver","PhoneStateReceiver PhoneStateReceiver filter_dynamic: " + filter_dynamic);
registerReceiver(phoneStateReceiver, filter_dynamic);
}