Android 藍芽狀態獲取
阿新 • • 發佈:2019-01-30
1. 藍芽狀態是否連線:
public static boolean isBTConnected(){ BluetoothAdapter blueadapter = BluetoothAdapter.getDefaultAdapter(); //adapter也有getState(), 可獲取ON/OFF...其它狀態 int a2dp = blueadapter.getProfileConnectionState(BluetoothProfile.A2DP); //可操控藍芽裝置,如帶播放暫停功能的藍芽耳機 int headset = blueadapter.getProfileConnectionState(BluetoothProfile.HEADSET); //藍芽頭戴式耳機,支援語音輸入輸出 int health = blueadapter.getProfileConnectionState(BluetoothProfile.HEALTH); return blueadapter != null && (a2dp == BluetoothAdapter.STATE_CONNECTED || headset == BluetoothAdapter.STATE_CONNECTED || health == BluetoothAdapter.STATE_CONNECTED); }
2. 藍芽變化監聽:
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); //filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(wifiReceiver, filter); //onReceive: else if(action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)){ int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); boolean enabled = (state == BluetoothAdapter.STATE_ON); }else if(action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)){ int status = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.STATE_DISCONNECTED); btConnected = (status == BluetoothAdapter.STATE_CONNECTED); }
感謝:
http://blog.csdn.net/memoryjs/article/details/42968823