Android bluetooth裝置狀態監聽
阿新 • • 發佈:2019-02-07
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) { case0: //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; } } } }