首页 > 系统 > Android > 正文

Android bluetooth设备状态监听

2019-11-09 16:18:37
字体:
来源:转载
供稿:网友
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;            }        }    }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表