zoukankan      html  css  js  c++  java
  • BluetoothMonitorReceiver

    package com.android.demo.lileidemo.listener;

    import android.bluetooth.BluetoothA2dp;
    import android.bluetooth.BluetoothA2dpSink;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothHeadset;
    import android.bluetooth.BluetoothHeadsetClient;
    import android.bluetooth.BluetoothHeadsetClientCall;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Handler;

    import com.android.demo.lileidemo.MyApplication;
    import com.android.demo.lileidemo.constant.AppConstants;

    import java.lang.reflect.Method;
    import java.util.Set;

    /**
    * date: 04/02/2020.
    * author: lilei.
    * Android8.0 should dynamic registration monitoring.
    */
    public class BluetoothMonitorReceiver extends BroadcastReceiver {
    private static final String TAG = AppConstants.APP_TAG + "BluetoothMonitorReceiver ";
    private Handler mWorker;
    private static volatile BluetoothMonitorReceiver mInstance;
    //private IIviStateChangeListener mIviStateChangeListener;
    private BluetoothHeadsetClientCall mHeadsetClientCall;
    private int mCurrentCallState;

    public BluetoothMonitorReceiver() {
    mWorker = new Handler();
    }

    /**
    * get Instance.
    *
    * @return instance.
    */
    public static BluetoothMonitorReceiver getInstance() {
    if (mInstance == null) {
    synchronized (BluetoothMonitorReceiver.class) {
    if (mInstance == null) {
    mInstance = new BluetoothMonitorReceiver();
    }
    }
    }
    return mInstance;
    }

    /**
    * Register Ivi State Change Listener and start Bluetooth monitor.
    *
    * @param iviStateChangeListener iviStateChangeListener.
    */
    // public void registerIviStateChangeListener(
    // IIviStateChangeListener iviStateChangeListener) {
    // LogUtil.d(TAG + "registerIviStateChangeListener()");
    // mIviStateChangeListener = iviStateChangeListener;
    // registerReceiver();
    // }

    /**
    * registerReceiver.
    */
    private void registerReceiver() {
    LogUtil.d(TAG + "registerReceiver private");
    IntentFilter intentFilter = new IntentFilter();
    //Monitor Bluetooth off and on.
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    //Monitor the connection status between Bluetooth device and app.
    intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    // intentFilter.addAction(BluetoothHeadsetClient.ACTION_CALL_CHANGED);
    // intentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
    // intentFilter.addAction(BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED);
    //for phone
    //intentFilter.addAction("android.bluetooth.pbap.profile.action.CONNECTION_STATE_CHANGED");
    intentFilter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
    //for media
    //intentFilter.addAction(BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED);
    intentFilter.addAction(BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED);
    // Registered broadcasting.
    MyApplication.getAppContext().registerReceiver(BluetoothMonitorReceiver.getInstance(),
    intentFilter);
    }

    public void registerPadReceiver() {
    String padBtPhoneAction = BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED;
    String padBtMediaAction = BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED;
    LogUtil.d(TAG + "registerPadReceiver private");
    IntentFilter intentFilter = new IntentFilter();
    //Monitor Bluetooth off and on.
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    //Monitor the connection status between Bluetooth device and app.
    intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);

    //for phone
    intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
    //for media
    intentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
    // Registered broadcasting.
    MyApplication.getAppContext().registerReceiver(BluetoothMonitorReceiver.getInstance(),
    intentFilter);
    }

    /**
    * unregisterReceiver.
    */
    public void unregisterReceiver() {
    MyApplication.getAppContext().unregisterReceiver(BluetoothMonitorReceiver.getInstance());
    }

    @Override
    public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    int bluetoothA2dpState = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
    int bluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
    int bluetoothDeviceState = intent.getIntExtra(BluetoothDevice.EXTRA_DEVICE, -1);
    LogUtil.d(TAG + "*# onReceive 0 begin action:" + action
    + " bluetoothA2dpState:" + bluetoothA2dpState + " bluetoothHeadsetState:" + bluetoothHeadsetState
    + " bluetoothDeviceState:" + bluetoothDeviceState);
    if (action == null) {
    return;
    }
    switch (action) {
    case BluetoothAdapter.ACTION_STATE_CHANGED:
    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
    switch (blueState) {
    case BluetoothAdapter.STATE_TURNING_ON:
    LogUtil.d(TAG + "onReceive STATE_TURNING_ON");
    break;
    case BluetoothAdapter.STATE_ON:
    LogUtil.d(TAG + "onReceive STATE_ON");
    break;
    case BluetoothAdapter.STATE_TURNING_OFF:
    LogUtil.d(TAG + "onReceive STATE_TURNING_OFF");
    break;
    case BluetoothAdapter.STATE_OFF:
    LogUtil.d(TAG + "onReceive STATE_OFF");
    //todo for bluetooth disconnected
    break;
    default:
    break;
    }
    break;

    case BluetoothDevice.ACTION_ACL_CONNECTED:
    LogUtil.d(TAG + "onReceive ACTION_ACL_CONNECTED");
    mWorker.postDelayed(new Runnable() {
    @Override
    public void run() {
    getConnectedBluetooth();
    }
    }, 1000);
    break;

    case BluetoothDevice.ACTION_ACL_DISCONNECTED:
    LogUtil.d(TAG + "onReceive ACTION_ACL_DISCONNECTED");
    //todo for bluetooth disconnected
    break;
    case BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED://for connect phone.
    processConnectPhone(bluetoothHeadsetState);
    break;
    case BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED: //for connect media.
    processConnectMedia(bluetoothHeadsetState);
    break;
    default:
    break;
    }
    }

    private void getConnectedBluetooth() {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    //Get BluetoothAdapter Class obj.
    Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;
    try { //Get Bluetooth status.
    Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState",
    (Class[]) null);
    //Open permission.
    method.setAccessible(true);
    int state = (int) method.invoke(adapter, (Object[]) null);
    LogUtil.d(TAG + "getConnectedBluetooth() state:" + state);
    LogUtil.d(TAG + "getConnectedBluetooth() state:" + state
    + " stateSTATE_CONNECTED:" + BluetoothAdapter.STATE_CONNECTED
    + " STATE_CONNECTING:" + BluetoothAdapter.STATE_CONNECTING
    + " STATE_DISCONNECTED:" + BluetoothAdapter.STATE_DISCONNECTED);

    Set<BluetoothDevice> devices = adapter.getBondedDevices();
    LogUtil.d(TAG + "getConnectedBluetooth devices.size:" + devices.size());

    for (BluetoothDevice device : devices) {
    Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected",
    (Class[]) null);
    method.setAccessible(true);
    boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
    LogUtil.d(TAG + "getConnectedBluetooth() isConnected:" + isConnected
    + " getAddress:" + device.getAddress() + " getName:" + device.getName());
    if (isConnected) {
    LogUtil.d(TAG + "getConnectedBluetooth() ** Connected getAddress:"
    + device.getAddress() + " getName:" + device.getName() + " getBondState:"
    + device.getBondState() + " getType:" + device.getType());
    //todo for bluetooth connected
    }
    }

    } catch (Exception e) {
    LogUtil.d(TAG + "getConnectedBluetooth() error:" + e.toString());
    e.printStackTrace();
    }
    }

    private void processConnectPhone(int bluetoothHeadsetState) {
    LogUtil.d(TAG + "processConnectPhone 00 bluetoothHeadsetState:" + bluetoothHeadsetState);
    switch (bluetoothHeadsetState) {
    case BluetoothHeadset.STATE_DISCONNECTED:
    LogUtil.d(TAG + "processConnectPhone 01 STATE_DISCONNECTED:");
    // if (null != mIviStateChangeListener) {
    // mIviStateChangeListener.onBluetoothStateChange(
    // AppConstants.IVI_BT_DISCONNECT_PHONE);
    // }
    break;
    case BluetoothHeadset.STATE_CONNECTED:
    LogUtil.d(TAG + "processConnectPhone 02 STATE_CONNECTED:");
    // if (null != mIviStateChangeListener) {
    // mIviStateChangeListener.onBluetoothStateChange(
    // AppConstants.IVI_BT_CONNECT_PHONE);
    // }
    break;
    default:
    break;
    }
    }

    private void processConnectMedia(int bluetoothHeadsetState) {
    LogUtil.d(TAG + "processConnectMedia 00 bluetoothHeadsetState:" + bluetoothHeadsetState);
    switch (bluetoothHeadsetState) {
    case BluetoothHeadset.STATE_DISCONNECTED:
    LogUtil.d(TAG + "processConnectMedia 01 STATE_DISCONNECTED:");
    // if (null != mIviStateChangeListener) {
    // mIviStateChangeListener.onBluetoothStateChange(
    // AppConstants.IVI_BT_DISCONNECT_MEDIA);
    // }
    break;
    case BluetoothHeadset.STATE_CONNECTED:
    LogUtil.d(TAG + "processConnectMedia 02 STATE_CONNECTED:");
    // if (null != mIviStateChangeListener) {
    // mIviStateChangeListener.onBluetoothStateChange(
    // AppConstants.IVI_BT_CONNECT_MEDIA);
    // }
    break;
    default:
    break;
    }
    }
    }
  • 相关阅读:
    Attributes.Add用途与用法
    Reapter控件中更换Td背景色
    SQL SERVER查询时间条件式写法
    C# Cache何时使用及使用方法
    C#中Cache用法
    用sql语句将两个时间相减,得到时间距的DateDiff()函数
    HTML5 带进度条的异步文件上传原理
    Node环境Grunt开发流
    HTML5的Web SQL Databases(html5 本地数据库)API
    移动端范围拖动选择效果
  • 原文地址:https://www.cnblogs.com/adamli/p/13140479.html
Copyright © 2011-2022 走看看