0

私はAndroid apiサンプルのブルートゥースアプリケーションを使用しています。ユーザーがデバイスを選択すると、ペアリングされたデバイスにメッセージを送信しようとすると、ペアリングされたブルートゥースデバイスがリストに表示されます。ユーザーがペアリングされたデバイスをチェックし、編集フィールドにメッセージを入力してから送信ボタンをクリックしたときの仕様を開発したいと思います。ユーザーが送信ボタンをクリックすると、ユーザーは選択したデバイスに接続し、そのメッセージをそのデバイスに送信できます。成功したらすぐに接続を閉じます。次のようにコードを実装しました

 ListView listDevicesFound;
 ArrayAdapter<String> btArrayAdapter;
 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set up the window layout
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    btArrayAdapter = new ArrayAdapter<String>(BluetoothChat.this, android.R.layout.simple_list_item_multiple_choice);
    listDevicesFound.setAdapter(btArrayAdapter);
    listDevicesFound.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listDevicesFound.setSelected(true);
    listDevicesFound.setClickable(true);
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
      if (pairedDevices.size() > 0) {
          for (BluetoothDevice device : pairedDevices) {
           String deviceBTName = device.getName();
           String deviceBTAddress = device.getAddress();
           btArrayAdapter.add(deviceBTName + "\n"  + deviceBTAddress);
          }
      }

      mSendButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {


          SparseBooleanArray checked = listDevicesFound.getCheckedItemPositions();
            Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
            Log.v("prasad", "devices.size()===>>>"+devices.size());
            for (int j=0;j<=devices.size();j++) 
               {
                    System.out.println(j);
                    if(checked.get(j))
                    {
                        String devadd= listDevicesFound.getItemAtPosition(j).toString();
                        String devaddress=devadd.substring(0,devadd.length()-17);
                        Log.v("prasad", "address===>>>"+devaddress);


                        /*
                         *BluetoothDevice:
                         *Represents a remote Bluetooth device. 
                         *A BluetoothDevice lets you create a connection with the respective device 
                         *or query information about it, such as the name, address, class, 
                         *and bonding state 
                         */

                        for(BluetoothDevice itDevices:devices)
                        {
                            Log.v("prasad", "itDevices.getAddress()===>>>"+itDevices.getAddress());
                            if(devadd.endsWith(itDevices.getAddress()))
                            {



                                            Log.v("1111","Here the adderess of selected device :"+itDevices.getAddress());


             }
         }
      }
   }




         //Please help on following steps
         //1.How to connect to a selected device code here                

         String message = messageEdit.getText().toString();
         //2.How to send the message to selected device here

         //3.How to close the connection with selected device here



            }
    });


  }  

コーディング行で私のコメントに従ってください

どんな体でも私を助けてください....

4

1 に答える 1

1

Bluetooth チャットのサンプル アプリはご覧になりましたか? それはあなたがやろうとしていることと非常に近いと思います: http://developer.android.com/resources/samples/BluetoothChat/index.html

また、Android Bluetooth デベロッパー ガイドには、あなたが尋ねた特定の質問に関連するコード サンプルがあります。

//1.ここで選択したデバイス コードに接続する方法
http://developer.android.com/guide/topics/wireless/bluetooth.html#ConnectingDevices BluetoothServerSocket で accept() を呼び出して着信をリッスンする方法を説明します一方の側で接続し、もう一方の側で BluetoothSocket で connect() を呼び出します。

//2.ここで選択したデバイスにメッセージを送信する方法 http://developer.android.com/guide/topics/wireless/bluetooth.html#ManagingAConnection ソケットを I/O ストリームに接続してから読み取り/書き込みを呼び出す方法について説明しますストリームで。

//3.ここで選択したデバイスとの接続を閉じる方法 BluetoothSockets で close() を呼び出すだけです。通常、これは 1 つのスレッドから行うため、Bluetooth スレッドで保留中の操作が I/O 例外をスローします。

ただし、上記のチャット サンプルと開発者ガイドを読むことをお勧めします。

于 2012-04-06T17:18:17.357 に答える