2012-04-06 8 views
0

私はアンドロイドのapiサンプルから青い歯のアプリケーションを持っています。ユーザーがデバイスを選択すると、リストに青い歯のペアのデバイスが表示されます。選択したペアデバイスにメッセージを送信しようとすると、そのメッセージがそのデバイスに配信されます。この仕様は、ユーザーがペアリングされたデバイスを確認した後、編集フィールドにメッセージを入力してから送信ボタンをクリックするように開発したいと思います。ユーザーが送信ボタンをクリックすると、選択したデバイスに接続して、配信が成功した場合は、すぐに接続を閉じます。どのようにデバイスを接続するメッセージを送信し、アンドロイドアプリケーションのBlueToothの接続を閉じますか?

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 



      } 
    }); 


    } 

を次のように 私はすべてのボディが私を助けてくださいラインに

コーディングで私のコメントに従ってくださいコードを実装している....

答えて

1

は、あなたは、Bluetoothチャットサンプルアプリを見たことがありますか?私はそれはあなたがやろうとしているものに非常に近いと思う:に接続するために

//1.How: http://developer.android.com/resources/samples/BluetoothChat/index.html

はまた、AndroidのBluetoothの開発者ガイドは、あなたが尋ねた特定の質問に関連するコードサンプルを持っていますここで選択したデバイスコード
http://developer.android.com/guide/topics/wireless/bluetooth.html#ConnectingDevices BluetoothServerSocketでaccept()を呼び出して一方の側で受信をリスンし、もう一方の側のBluetoothSocketでconnect()を呼び出す方法を説明します。

//2.Howが http://developer.android.com/guide/topics/wireless/bluetooth.html#ManagingAConnection ここで選択したデバイスにメッセージを送信するためには、I/Oストリームに自分のソケットを装着して、ストリーム上の読み取り/書き込みを呼び出すことについて説明しています。

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

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

関連する問題