藍牙權限 <uses-permission android:name="android.permission.BLUETOOTH" />
1、監聽手機本身藍牙狀態的廣播
手機藍牙開啟關閉時發送
action: 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("aaa", "STATE_OFF 手機藍牙關閉"); break; case BluetoothAdapter.STATE_TURNING_OFF: Log.d("aaa", "STATE_TURNING_OFF 手機藍牙正在關閉"); break; case BluetoothAdapter.STATE_ON: Log.d("aaa", "STATE_ON 手機藍牙開啟"); break; case BluetoothAdapter.STATE_TURNING_ON: Log.d("aaa", "STATE_TURNING_ON 手機藍牙正在開啟"); break; } }
2、監聽藍牙設備配對狀態的廣播
藍牙設備配對和解除配對時發送
action: 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("aaa", "device name: " + name); int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1); switch (state) { case BluetoothDevice.BOND_NONE: Log.d("aaa", "BOND_NONE 刪除配對"); break; case BluetoothDevice.BOND_BONDING: Log.d("aaa", "BOND_BONDING 正在配對"); break; case BluetoothDevice.BOND_BONDED: Log.d("aaa", "BOND_BONDED 配對成功"); break; } }
3、監聽藍牙設備連接和連接斷開的廣播
藍牙設備連接上和斷開連接時發送, 這兩個監聽的是底層的連接狀態
action: BluetoothDevice.ACTION_ACL_CONNECTED BluetoothDevice.ACTION_ACL_DISCONNECTED
if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d("aaa", device.getName() + " ACTION_ACL_CONNECTED"); } else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d("aaa", device.getName() + " ACTION_ACL_DISCONNECTED"); }
BluetoothClass 可以獲取藍牙設備的類型
如果想獲取當前已連接上的所有藍牙設備,可以在這兩個廣播中手動維護一個連接設備的列表。
像下面這樣:
/** * 記錄當前正在連接的所有藍牙輸入設備 */ public List<BluetoothDevice> connectedBluetoothDevices = new ArrayList<BluetoothDevice>(); if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_CONNECTED)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (isInputDevice(device)) { List<BluetoothDevice> connectedBluetoothDevices = ((MyApplication) getApplication()).connectedBluetoothDevices; if (!connectedBluetoothDevices.contains(device)) { connectedBluetoothDevices.add(device); } } } else if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (isInputDevice(device)) { List<BluetoothDevice> connectedBluetoothDevices = ((MyApplication) getApplication()).connectedBluetoothDevices; connectedBluetoothDevices.remove(device); } } /** * 判斷藍牙設備是否是輸入設備,這里認為 PERIPHERAL是輸入設備 */ private boolean isInputDevice(BluetoothDevice device) { int deviceMajorClass = device.getBluetoothClass().getMajorDeviceClass(); if (deviceMajorClass == BluetoothClass.Device.Major.PERIPHERAL) { return true; } return false; }