網(wǎng)站首頁 編程語言 正文
本文要實(shí)現(xiàn)的是在 android 8.0 的平臺(tái)上,藍(lán)牙遙控器與TV自動(dòng)配對(duì),具體就是在TV端打開配對(duì)界面,TV端開始搜索遠(yuǎn)程藍(lán)牙設(shè)備,按下遙控器按鍵讓藍(lán)牙遙控器進(jìn)入對(duì)碼模式,此時(shí)藍(lán)牙遙控器就能作為一個(gè)遠(yuǎn)程藍(lán)牙設(shè)備被發(fā)現(xiàn),TV端掃描到這個(gè)遠(yuǎn)程藍(lán)牙設(shè)備(藍(lán)牙遙控器),就會(huì)自動(dòng)進(jìn)行配對(duì)連接。
話不多說,直接上代碼分析。
public class RcConnectActivity extends Activity { ? ? ?? ?private static final String TAG = "RcConnectActivity"; ?? ??? ?private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); ?? ? ? ?private BluetoothDevice mBluetoothDevice; ?? ??? ?private BluetoothReceiver mBluetoothReceiver = null; ?? ??? ?private boolean isConnected = false; ?? ? ?? ? ? ?@Override ?? ? ? ?protected void onCreate(Bundle savedInstanceState) { ?? ??? ??? ?Log.d(TAG, "onCreate"); ?? ? ? ? ? ?super.onCreate(savedInstanceState); ?? ? ? ? ? ?setContentView(R.layout.rc_connect); ?? ??? ??? ?registerReceiver(); ?? ??? ??? ?if (mBluetoothAdapter != null) { ?? ??? ??? ??? ?if (mBluetoothAdapter.isEnabled()) { ?? ??? ??? ??? ??? ?mBluetoothAdapter.startDiscovery(); ?? ??? ??? ??? ??? ?Log.d(TAG, "mBluetoothAdapter.startDiscovery"); ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?mBluetoothAdapter.enable(); ?? ??? ??? ??? ??? ?Log.d(TAG, "mBluetoothAdapter.enable"); ?? ??? ??? ??? ?} ?? ??? ??? ?} else { ?? ??? ??? ??? ?Toast.makeText(this, R.string.bluetooth_tip, Toast.LENGTH_SHORT).show(); ?? ??? ??? ?} ?? ? ? ?}
首先我們要注冊(cè)一個(gè)廣播接收器,用來接收藍(lán)牙掃描搜索配對(duì)過程中一些藍(lán)牙相關(guān)的廣播,以便進(jìn)行相對(duì)應(yīng)的操作。
public void registerReceiver() { ?? ??? ?Log.d(TAG, "registerReceiver"); ? ? ? ? IntentFilter filter = new IntentFilter(); ? ? ? ? filter.addAction(BluetoothDevice.ACTION_FOUND); ? ? ? ? filter.addAction(BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED); ? ? ? ? filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); ? ? ? ? filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); ?? ??? ?mBluetoothReceiver = new BluetoothReceiver(); ? ? ? ? registerReceiver(mBluetoothReceiver, filter); ? ? }
BluetoothDevice.ACTION_FOUND 也就是 android.bluetooth.device.action.FOUND,當(dāng)發(fā)現(xiàn)遠(yuǎn)程藍(lán)牙設(shè)備的時(shí)候,系統(tǒng)就會(huì)發(fā)出這條廣播。接收這條廣播需要以下權(quán)限。
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED:當(dāng)藍(lán)牙連接狀態(tài)改變時(shí),就會(huì)發(fā)送此廣播。
BluetoothAdapter.ACTION_STATE_CHANGED:也就是 android.bluetooth.adapter.action.STATE_CHANGED 當(dāng)本地藍(lán)牙適配器的狀態(tài)改變時(shí),比如打開藍(lán)牙或者關(guān)閉藍(lán)牙的時(shí)候,就會(huì)發(fā)送此廣播。
BluetoothAdapter.ACTION_DISCOVERY_FINISHED:當(dāng)本地藍(lán)牙適配器完成設(shè)備掃描搜索過程的時(shí)候,就會(huì)發(fā)送此廣播。
注冊(cè)完廣播接著就是通過 BluetoothAdapter.getDefaultAdapter() 來獲取本地藍(lán)牙適配器,如果硬件不支持藍(lán)牙的話,那么返回值為null。如果能獲取到,證明TV端是有可用的藍(lán)牙模塊,接著通過 isEnabled() 這個(gè)方法來判斷TV端的藍(lán)牙模塊是否已經(jīng)打開并且可以使用,相當(dāng)于 getBluetoothState() == STATE_ON 。如果已經(jīng)打開藍(lán)牙,那么就可以通過 startDiscovery() 進(jìn)行掃描藍(lán)牙設(shè)備,否則就通過 enable() 來打開藍(lán)牙。
startDiscovery() 需要<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
這個(gè)權(quán)限。
private class BluetoothReceiver extends BroadcastReceiver { ? ? ? ? @Override ? ? ? ? public void onReceive(Context context, Intent intent) { ?? ??? ??? ? ?? ??? ??? ?String action = intent.getAction();? ?? ??? ??? ?Log.d(TAG, "action = " + action); ?? ??? ??? ?if(BluetoothDevice.ACTION_FOUND.equals(action)){? ?? ??? ??? ??? ?mBluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); ?? ??? ??? ??? ?Log.d(TAG, "find device : " + "[ "+ mBluetoothDevice.getName() +" ]" + ":" + mBluetoothDevice.getAddress()); ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (mBluetoothDevice.getName() == null || !mBluetoothDevice.getName().equals("RCSP")) { ?? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ?} else? ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?if (mBluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) { ?? ??? ??? ??? ??? ??? ?Log.d(TAG, "attemp to bond: " + "[ " + mBluetoothDevice.getName() + " ]"); ?? ??? ??? ??? ??? ??? ?try { ?? ??? ??? ??? ??? ??? ??? ?mBluetoothDevice.createBond(); ?? ??? ??? ??? ??? ??? ??? ?isConnected = true; ?? ??? ??? ??? ??? ??? ?} catch (Exception e) { ?? ??? ??? ??? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?}? ?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ?} ?? ??? ??? ?} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { ?? ??? ??? ??? ?int status = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0); ?? ??? ??? ??? ?if (BluetoothAdapter.STATE_ON == status) { ?? ??? ??? ??? ??? ?mBluetoothAdapter.startDiscovery(); ?? ??? ??? ??? ??? ?Log.d(TAG, "mBluetoothAdapter.startDiscovery---STATE_ON"); ?? ??? ??? ??? ?} ?? ??? ??? ?} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { ?? ??? ??? ??? ?if (!isConnected) { ?? ??? ??? ??? ??? ?mBluetoothAdapter.startDiscovery(); ?? ??? ??? ??? ??? ?Log.d(TAG, "mBluetoothAdapter.startDiscovery---ACTION_DISCOVERY_FINISHED"); ?? ??? ??? ??? ?} ?? ??? ??? ?} else if (BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED.equals(action)) { ? ? ? ? ? ? ? ? int newState = intent.getExtras().getInt(BluetoothProfile.EXTRA_STATE); ? ? ? ? ? ? ? ? switch (newState) { ? ? ? ? ? ? ? ? ? ? case BluetoothProfile.STATE_CONNECTING: ? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "CONNECTING"); ?? ??? ??? ??? ??? ??? ?Toast.makeText(context, R.string.bluetooth_connecting, Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case BluetoothProfile.STATE_CONNECTED: ?? ??? ??? ??? ??? ??? ?Log.d(TAG, "CONNECTED"); ?? ??? ??? ??? ??? ??? ?Toast.makeText(context, R.string.bluetooth_connected, Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? ? ? RcConnectActivity.this.finish(); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ?? ??? ??? ?}?? ? ? ? ? ? } ? ? }
流程分析:
1、如果TV端的藍(lán)牙模塊已經(jīng)打開,那么就執(zhí)行 startDiscovery(),否則通過 enable() 打開藍(lán)牙,此時(shí)會(huì)接收到 BluetoothAdapter.ACTION_STATE_CHANGED 這條廣播。藍(lán)牙有四種狀態(tài),分別是STATE_OFF、STATE_TURNING_ON、STATE_ON、STATE_TURNING_OFF。當(dāng)藍(lán)牙狀態(tài)為STATE_ON,表示藍(lán)牙已經(jīng)打開并且已經(jīng)準(zhǔn)備就緒,此時(shí)才可以進(jìn)行startDiscovery(),否則 startDiscovery() 會(huì)返回false,無法掃描搜索遠(yuǎn)程藍(lán)牙設(shè)備。
2、掃描搜索到遠(yuǎn)程設(shè)備之后,判斷是不是目標(biāo)設(shè)備,目標(biāo)設(shè)備藍(lán)牙遙控器的名字為 RCSP 。如果 getName() 獲取到的名字為null,或者不是 RCSP,直接 return,不進(jìn)行任何操作。android 8.0 要對(duì) getName() 為 null 進(jìn)行處理,不然程序會(huì)運(yùn)行出錯(cuò)。如果搜索到目標(biāo)設(shè)備,通過 createBond() 方法,實(shí)現(xiàn)自動(dòng)配對(duì)。
3、startDiscovery() 會(huì)進(jìn)行大約12秒的掃描搜索,有可能此時(shí)我們的目標(biāo)設(shè)備還沒有進(jìn)入對(duì)碼模式,還不能被TV端發(fā)現(xiàn),從而也無法自動(dòng)配對(duì)。當(dāng)掃描搜索完成之后,會(huì)發(fā)送 BluetoothAdapter.ACTION_DISCOVERY_FINISHED 這條廣播,此時(shí)我們?cè)谂袛嗄繕?biāo)設(shè)備是否已經(jīng)配對(duì)連接,如果沒有,再次調(diào)用 startDiscovery() 進(jìn)行掃描搜索。
4、當(dāng)目標(biāo)設(shè)備在進(jìn)行自動(dòng)配對(duì)的時(shí)候,我們通過接收BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED 這條廣播,來判斷目標(biāo)設(shè)備的狀態(tài),并用 Toast 提示配對(duì)是否成功。
原文鏈接:https://blog.csdn.net/lojotee/article/details/103693201
相關(guān)推薦
- 2022-07-16 Date 轉(zhuǎn)為 LocalDate
- 2022-06-15 GO語言類型查詢類型斷言示例解析_Golang
- 2022-11-23 Python?os.listdir與os.walk實(shí)現(xiàn)獲取路徑詳解_python
- 2023-03-25 Python?self參數(shù)詳細(xì)介紹_python
- 2022-05-20 springCloud_ Ribbon負(fù)載均衡
- 2022-07-15 Android自定義view繪制表格的方法_Android
- 2022-04-10 vscode快速輸出格式化console.log
- 2022-08-02 Redis跳躍表的基本原理和實(shí)現(xiàn)_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支