日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

蓝牙开关的监听

發布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 蓝牙开关的监听 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

藍牙權限?<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; } 發一下我寫的 private void registerBoradcastReceiver() { //注冊監聽 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; } } };

總結

以上是生活随笔為你收集整理的蓝牙开关的监听的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。