蓝牙开关的监听
藍牙權(quán)限?<uses-permission android:name="android.permission.BLUETOOTH" />
1、監(jiān)聽手機本身藍牙狀態(tài)的廣播
手機藍牙開啟關閉時發(fā)送
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、監(jiān)聽藍牙設備配對狀態(tài)的廣播
藍牙設備配對和解除配對時發(fā)送
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、監(jiān)聽藍牙設備連接和連接斷開的廣播
藍牙設備連接上和斷開連接時發(fā)送, 這兩個監(jiān)聽的是底層的連接狀態(tài)
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; } 發(fā)一下我寫的 private void registerBoradcastReceiver() { //注冊監(jiān)聽 getApplication().registerReceiver(stateChangeReceiver, makeFilter()); } private IntentFilter makeFilter() { IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); return filter; } private BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d("藍牙", "" + intent.getAction()); switch (intent.getAction()) { case BluetoothAdapter.ACTION_STATE_CHANGED: int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0); Log.d("藍牙", "藍牙" + blueState); switch (blueState) { case BluetoothAdapter.STATE_TURNING_ON: Log.d("STATE_TURNING_ON", "藍牙"); break; case BluetoothAdapter.STATE_ON: Log.d("STATE_ON", "藍牙"); break; case BluetoothAdapter.STATE_TURNING_OFF: Log.d("STATE_TURNING_OFF", "藍牙"); //藍牙開關手動關閉 會收到這條廣播 break; case BluetoothAdapter.STATE_OFF: Log.d("STATE_OFF", "藍牙"); break; } break; } } };總結(jié)
- 上一篇: TextClock不管是24小时还是12
- 下一篇: 数字基础设施可视化管理,任重而道远