Android bluetooth设备状态监听

来源:互联网 发布:java 线程池原理 编辑:程序博客网 时间:2024/06/05 03:05
public class BluetoothHeadsetBroadcastReceiver extends BroadcastReceiver {    public BluetoothHeadsetBroadcastReceiver() {    }    public void onReceive(Context context, Intent intent) {        String action = intent.getAction();        int state;        BluetoothDevice device;        if (action.equals("android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED")) {            state = intent.getIntExtra("android.bluetooth.profile.extra.STATE", 0);            device = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");            switch (state) {                case 0:                    //disconnected                    break;                case 1:                    //connecting                    break;                case 2:                    //connected                    break;                case 3:                    //disconnecting                    break;                default:                    //Bluetooth device unknown state            }        } else if (action.equals("android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED")) {            state = intent.getIntExtra("android.bluetooth.profile.extra.STATE", 10);            device = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");            switch (state) {                case 10:                    //Bluetooth audio device disconnected                    break;                case 11:                    //connecting                    break;                case 12:                    //connected                    break;                default:                    //unknown state            }        } else if (action.equals("android.media.ACTION_SCO_AUDIO_STATE_UPDATED")) {            state = intent.getIntExtra("android.media.extra.SCO_AUDIO_STATE", 0);            switch (state) {                case -1:                    //Bluetooth SCO device error                    break;                case 0:                    //Bluetooth SCO device disconnected                    break;                case 1:                    //Bluetooth SCO device connected                    break;                case 2:                    //Bluetooth SCO device connecting                    break;                default:                    //Bluetooth SCO device unknown event;            }        }    }}
0 0