欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?8.0實現(xiàn)藍牙遙控器自動配對

 更新時間:2022年08月17日 11:58:22   作者:lojotee  
這篇文章主要為大家詳細介紹了Android?8.0實現(xiàn)藍牙遙控器自動配對,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文要實現(xiàn)的是在 android 8.0 的平臺上,藍牙遙控器與TV自動配對,具體就是在TV端打開配對界面,TV端開始搜索遠程藍牙設(shè)備,按下遙控器按鍵讓藍牙遙控器進入對碼模式,此時藍牙遙控器就能作為一個遠程藍牙設(shè)備被發(fā)現(xiàn),TV端掃描到這個遠程藍牙設(shè)備(藍牙遙控器),就會自動進行配對連接。

話不多說,直接上代碼分析。

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();
?? ??? ??? ?}
?? ? ? ?}

首先我們要注冊一個廣播接收器,用來接收藍牙掃描搜索配對過程中一些藍牙相關(guān)的廣播,以便進行相對應(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,當發(fā)現(xiàn)遠程藍牙設(shè)備的時候,系統(tǒng)就會發(fā)出這條廣播。接收這條廣播需要以下權(quán)限。

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED:當藍牙連接狀態(tài)改變時,就會發(fā)送此廣播。

BluetoothAdapter.ACTION_STATE_CHANGED:也就是 android.bluetooth.adapter.action.STATE_CHANGED 當本地藍牙適配器的狀態(tài)改變時,比如打開藍牙或者關(guān)閉藍牙的時候,就會發(fā)送此廣播。

BluetoothAdapter.ACTION_DISCOVERY_FINISHED:當本地藍牙適配器完成設(shè)備掃描搜索過程的時候,就會發(fā)送此廣播。

注冊完廣播接著就是通過 BluetoothAdapter.getDefaultAdapter() 來獲取本地藍牙適配器,如果硬件不支持藍牙的話,那么返回值為null。如果能獲取到,證明TV端是有可用的藍牙模塊,接著通過 isEnabled() 這個方法來判斷TV端的藍牙模塊是否已經(jīng)打開并且可以使用,相當于 getBluetoothState() == STATE_ON 。如果已經(jīng)打開藍牙,那么就可以通過 startDiscovery() 進行掃描藍牙設(shè)備,否則就通過 enable() 來打開藍牙。
startDiscovery() 需要<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />這個權(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端的藍牙模塊已經(jīng)打開,那么就執(zhí)行 startDiscovery(),否則通過 enable() 打開藍牙,此時會接收到 BluetoothAdapter.ACTION_STATE_CHANGED 這條廣播。藍牙有四種狀態(tài),分別是STATE_OFF、STATE_TURNING_ON、STATE_ON、STATE_TURNING_OFF。當藍牙狀態(tài)為STATE_ON,表示藍牙已經(jīng)打開并且已經(jīng)準備就緒,此時才可以進行startDiscovery(),否則 startDiscovery() 會返回false,無法掃描搜索遠程藍牙設(shè)備。

2、掃描搜索到遠程設(shè)備之后,判斷是不是目標設(shè)備,目標設(shè)備藍牙遙控器的名字為 RCSP 。如果 getName() 獲取到的名字為null,或者不是 RCSP,直接 return,不進行任何操作。android 8.0 要對 getName() 為 null 進行處理,不然程序會運行出錯。如果搜索到目標設(shè)備,通過 createBond() 方法,實現(xiàn)自動配對。

3、startDiscovery() 會進行大約12秒的掃描搜索,有可能此時我們的目標設(shè)備還沒有進入對碼模式,還不能被TV端發(fā)現(xiàn),從而也無法自動配對。當掃描搜索完成之后,會發(fā)送 BluetoothAdapter.ACTION_DISCOVERY_FINISHED 這條廣播,此時我們在判斷目標設(shè)備是否已經(jīng)配對連接,如果沒有,再次調(diào)用 startDiscovery() 進行掃描搜索。

4、當目標設(shè)備在進行自動配對的時候,我們通過接收BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED 這條廣播,來判斷目標設(shè)備的狀態(tài),并用 Toast 提示配對是否成功。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論