Android Bluetooth開発シリーズの検索Bluetoothデバイス

4142 ワード

重要な一歩
Bluetoothを開発する最初のステップはmanifestです.xmlファイルにBluetoothのアクセス権を追加



最初の行のコードは、プログラムがペアリングされたBluetoothデバイスに接続できることを示しています.すなわち、Bluetoothの2行目のコードを使用することを許可しているのは、プログラムがBluetoothデバイスを発見し、ペアリングできることを示しています.つまり、管理Bluetoothがこの2行のコードを追加すれば、Bluetoothの世界で「忌み嫌うことはありません」ことができます.
Bluetoothデバイスを最初に開く
周囲のBluetoothデバイスを検索するには、まず自分のデバイスのBluetoothデバイスを開き、Bluetoothを開く操作を実行する前に、まず自分のデバイスがBluetoothをサポートしているかどうかを判断します.
BluetoothAdapter bTAdatper = BluetoothAdapter.getDefaultAdapter();

Bluetoothがサポートされているかどうかを確認し、サポートされていない場合は、現在のデバイスがBluetoothをサポートしていないことを出力します.コードは次のとおりです.
if(bTAdatper==null){
            Toast.makeText(this,"          ",Toast.LENGTH_SHORT).show();
            }

Bluetoothが開いているかどうかを判断し、開いていない場合は、Bluetoothを開くことをサポートします.開くには2つの方法があります
bTAdatper.isEnabled()

および
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent)

2つの方法の違いは、2つ目の方法では、Bluetoothを開くかどうかをユーザーに尋ねるプロンプトボックスがポップアップされ、前者は直接開くことです.次に、独自のBluetoothを検出できるように設定できます.以下のコードがこの機能を実現します.
if (bTAdatper.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
          Intent intent  = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            //     150    
          intent .putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,150);
          startActivity(intent );
        }

第2ステップBluetoothデバイスの検索
この手順のコードは非常に簡単です.
bTAdatper.startDiscovery();
bTAdatper.cancelDiscovery()

Bluetoothデバイスを検索します.このプロセスは非同期で、次のブロードキャスト受信者を登録することで、デバイスが検索されたかどうかを監視できます.
        IntentFilter filter = new IntentFilter();  //        ,      
        filter.addAction(BluetoothDevice.ACTION_FOUND);//      
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//                     
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//                  
        registerReceiver(mReceiver, filter); / //  registerReceiver()  
    }

システムはこれらの状態を発行し、これらの状態を取得し、ブロードキャスト受信者によってスキャンされたBluetoothデバイスを表示し、1つのデバイスにスキャンされるたびに、このブロードキャスト(BluetoothDevice.ACTION_FOUNDE)を送信する.パラメータintentはBluetoothデバイスBluetoothデバイスを取得できます.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                //  Parcelable     Intent    ,        
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                /**
                 * //              
                 * getBondState()          ,BOND_BONDED           
                 */
                if (device.getBondState() != BluetoothDevice.BOND_BONDED)
                {
                    adapter.add(device);
                    adapter.notifyDataSetChanged();
                }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                Toast.makeText(MainActivity.this, "    ", Toast.LENGTH_SHORT).show();
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                Toast.makeText(MainActivity.this, "    ", Toast.LENGTH_SHORT).show();
            }
        }
    };

検索したデバイスをリストに追加して表示します.Bluetoothは、検索中にペアリングされたBluetoothデバイスを検索するので、以前にペアリングされたBluetoothデバイスを取得できます.
Set Devices = bTAdatper.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
              list.add(device);
            }
        }

Bluetoothで検索機能を実現
このシリーズの文章は小牛兄のシリーズの博文に重点を置いて感謝します:
https://blog.csdn.net/a1533588867/article/category/6402830