欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 9.0 蓝牙各种广播监听

程序员文章站 2024-03-24 23:28:16
...

蓝牙开关状态变化
BluetoothAdapter.ACTION_STATE_CHANGED

if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
    int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
            BluetoothAdapter.ERROR);
    switch (state) {
        case BluetoothAdapter.STATE_OFF:
            Log.d("BroadcastReceiver", "STATE_OFF 手机蓝牙关闭");
            break;
        case BluetoothAdapter.STATE_TURNING_OFF:
            Log.d("BroadcastReceiver", "STATE_TURNING_OFF 手机蓝牙正在关闭");
            break;
        case BluetoothAdapter.STATE_ON:
            Log.d("BroadcastReceiver", "STATE_ON 手机蓝牙开启");
            break;
        case BluetoothAdapter.STATE_TURNING_ON:
            Log.d("BroadcastReceiver", "STATE_TURNING_ON 手机蓝牙正在开启");
            break;
    }
}

蓝牙设备配对状态变化
BluetoothDevice.ACTION_BOND_STATE_CHANGED

if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    String name = device.getName();
    Log.d("BroadcastReceiver", "device name: " + name);
    int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
    switch (state) {
        case BluetoothDevice.BOND_NONE:
            Log.d("BroadcastReceiver", "BOND_NONE 删除配对");
            break;
        case BluetoothDevice.BOND_BONDING:
            Log.d("BroadcastReceiver", "BOND_BONDING 正在配对");
            break;
        case BluetoothDevice.BOND_BONDED:
            Log.d("BroadcastReceiver", "BOND_BONDED 配对成功");
            break;
    }
} 

蓝牙设备acl连接和连接断开
BluetoothDevice.ACTION_ACL_CONNECTED
BluetoothDevice.ACTION_ACL_DISCONNECTED

if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    Log.d("BroadcastReceiver", device.getName() + " ACTION_ACL_CONNECTED");
} else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    Log.d("BroadcastReceiver", device.getName() + " ACTION_ACL_DISCONNECTED");
}

蓝牙通话输出模式改变
BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED

String: BluetoothProfile.EXTRA_STATE
		BluetoothProfile.EXTRA_PREVIOUS_STATE

value: 	    
	/** The profile is in disconnected state */
    int STATE_DISCONNECTED = 0;
    /** The profile is in connecting state */
    int STATE_CONNECTING = 1;
    /** The profile is in connected state */
    int STATE_CONNECTED = 2;
    /** The profile is in disconnecting state */
    int STATE_DISCONNECTING = 3;

蓝牙远程广播连接状态
BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED

String: BluetoothProfile.EXTRA_STATE
		BluetoothProfile.EXTRA_PREVIOUS_STATE

value: 	    
	/** The profile is in disconnected state */
    int STATE_DISCONNECTED = 0;
    /** The profile is in connecting state */
    int STATE_CONNECTING = 1;
    /** The profile is in connected state */
    int STATE_CONNECTED = 2;
    /** The profile is in disconnecting state */
    int STATE_DISCONNECTING = 3;

接收上次执行的结果
BluetoothHeadsetClient.ACTION_RESULT

String: BluetoothHeadsetClient.EXTRA_RESULT_CODE
		BluetoothHeadsetClient.EXTRA_CME_CODE

value: 	    
	 * {@link #ACTION_RESULT_OK},
     * {@link #ACTION_RESULT_ERROR},
     * {@link #ACTION_RESULT_ERROR_NO_CARRIER},
     * {@link #ACTION_RESULT_ERROR_BUSY},
     * {@link #ACTION_RESULT_ERROR_NO_ANSWER},
     * {@link #ACTION_RESULT_ERROR_DELAYED},
     * {@link #ACTION_RESULT_ERROR_BLACKLISTED},
     * {@link #ACTION_RESULT_ERROR_CME}

最后一次语音号码
BluetoothHeadsetClient.ACTION_LAST_VTAG

   * {@link #EXTRA_NUMBER},
     * with a <code>String</code> value representing phone number.

通话状态改变
BluetoothHeadsetClient.ACTION_CALL_CHANGED

String: BluetoothHeadsetClient.EXTRA_CALL

value:
	BluetoothHeadsetClientCall 	    
	 /**
     * Call is active.
     */
    public static final int CALL_STATE_ACTIVE = 0;
    /**
     * Call is in held state.
     */
    public static final int CALL_STATE_HELD = 1;
    /**
     * Outgoing call that is being dialed right now.
     */
    public static final int CALL_STATE_DIALING = 2;
    /**
     * Outgoing call that remote party has already been alerted about.
     */
    public static final int CALL_STATE_ALERTING = 3;
    /**
     * Incoming call that can be accepted or rejected.
     */
    public static final int CALL_STATE_INCOMING = 4;
    /**
     * Waiting call state when there is already an active call.
     */
    public static final int CALL_STATE_WAITING = 5;
    /**
     * Call that has been held by response and hold
     * (see Bluetooth specification for further references).
     */
    public static final int CALL_STATE_HELD_BY_RESPONSE_AND_HOLD = 6;
    /**
     * Call that has been already terminated and should not be referenced as a valid call.
     */
    public static final int CALL_STATE_TERMINATED = 7;

更新AG(Audio Gateway)状态
BluetoothHeadsetClient.ACTION_AG_EVENT

String: 
	 * {@link #EXTRA_NETWORK_STATUS},
     * {@link #EXTRA_NETWORK_SIGNAL_STRENGTH},
     * {@link #EXTRA_NETWORK_ROAMING},
     * {@link #EXTRA_BATTERY_LEVEL},
     * {@link #EXTRA_OPERATOR_NAME},
     * {@link #EXTRA_VOICE_RECOGNITION},
     * {@link #EXTRA_IN_BAND_RING}
     * EXTRA_SUBSCRIBER_INFO
value:

蓝牙连接状态变化
BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED

String: BluetoothAdapter.EXTRA_CONNECTION_STATE
		BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE

value: 	    
	/** The profile is in disconnected state */
    int STATE_DISCONNECTED = 0;
    /** The profile is in connecting state */
    int STATE_CONNECTING = 1;
    /** The profile is in connected state */
    int STATE_CONNECTED = 2;
    /** The profile is in disconnecting state */
    int STATE_DISCONNECTING = 3;

蓝牙pbap状态变化
BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED

String: BluetoothProfile.EXTRA_STATE
		BluetoothAdapter.EXTRA_PREVIOUS_STATE

value: 	    
	/** The profile is in disconnected state */
    int STATE_DISCONNECTED = 0;
    /** The profile is in connecting state */
    int STATE_CONNECTING = 1;
    /** The profile is in connected state */
    int STATE_CONNECTED = 2;
    /** The profile is in disconnecting state */
    int STATE_DISCONNECTING = 3;

蓝牙a2dp状态变化
BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED

String: BluetoothProfile.EXTRA_STATE
		BluetoothAdapter.EXTRA_PREVIOUS_STATE

value: 	    
	/** The profile is in disconnected state */
    int STATE_DISCONNECTED = 0;
    /** The profile is in connecting state */
    int STATE_CONNECTING = 1;
    /** The profile is in connected state */
    int STATE_CONNECTED = 2;
    /** The profile is in disconnecting state */
    int STATE_DISCONNECTING = 3;

蓝牙名字变化
BluetoothDevice.ACTION_NAME_CHANGED

String: 
		BluetoothDevice.EXTRA_DEVICE

value: 

蓝牙开始扫描设备
BluetoothAdapter.ACTION_DISCOVERY_STARTED
蓝牙扫描结束
BluetoothAdapter.ACTION_DISCOVERY_FINISHED
蓝牙发现设备
BluetoothDevice.ACTION_FOUND
蓝牙音乐信息变化
AvrcpControllerService.ACTION_TRACK_EVENT

String: AvrcpControllerService.EXTRA_METADATA
		   AvrcpControllerService.EXTRA_PLAYBACK
value: 	    
	MediaMetadata metaData = intent.getParcelableExtra(EXTRA_METADATA);
    PlaybackState state = intent.getParcelableExtra(EXTRA_PLAYBACK);

转载:https://blog.csdn.net/geniushorse/article/details/108601349?spm=1001.2014.3001.5501