2014-10-05 1 views
0

Android에서 제공하는 P2P API를 살펴 보았습니다. 동료를 발견하고 그들과 연결하는 모든 단계를 알고 있습니다. 내 질문은 다른 장치가 내 장치에 어떤 장치에 의해 연결이 생성되었음을 어떻게 알 수 있습니까? bcz 연결 후 소켓을 사용하여 데이터를 전송합니다. 소켓이 작동하려면 클라이언트가 서버의 IP와 포트를 알아야합니다. 이 asdress는 클라이언트 측에서 수동으로 입력해야합니까? 내 앱의 흐름이 기기 A와 비슷해야합니다. 기기 A가 기기를 발견하고 하나 이상의 기기에 내 앱이 설치되어있을 수 있습니다. A는 하나의 장치를 선택하고 연결 요청을 전송합니다. 다른 장치는 요청을 수락/거부 할 수 있습니다. 받아 들여지면 두 장치는 서버와 다른 클라이언트로 소켓 하나를 통해 연결되어야합니다. 내가 원하는 것을 잘 설명했으면 좋겠다.Android P2P 클라이언트가 기기와의 연결 상태를 알 수있는 방법은 무엇입니까?

답변

0

저는 현재 이와 같은 앱을 제작 중이며이 프로세스를 간소화하는 데 "최선의 방법"을 말할 수는 없지만 P2P 서비스를 종료하거나 일시 중지하지 못하게하기 때문에 서비스를 만드는 것이 좋습니다. 신청.

경로를 선택하지 않으면 어댑터를 사용하여 장치에 연결된 모든 장치와 주소를 포함하는 listView를 자동으로 업데이트 할 수 있습니다.

public class myActivity extends Activity 
{ 
    WifiP2pManager.Channel myChannel; //This channel is created and passed to system services in order for WIFI_P2P to work 
    WifiP2pManager myManager; //This manager is declared to get the required channel object 
    List connections;//connections holds all of the connections that the host is dealing with. 
    ListView listView;//listView which we will use to display progress bars 
    ArrayAdapter<String> adapter;//adapter which is used to update and display the connections 

...

protected void onCreate 
{ 
    connections = new ArrayList();//make an arraylist for the connections 
    connections.add("No Connected Devices");//say there's no connections as a default 
    listView = (ListView)findViewById(R.id.connectionsListView);//initialize the list view 
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, connections);//create an array adapter 
    listView.setAdapter(adapter);//attach the adapter to the listView 

그리고 마지막으로,

public class myBroadcastReceiver 
{ 
    else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) 
    { 
     // This is when new connections or disconnections occur 
     //WifiP2pInfo info = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO); 
     WifiP2pGroup group = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP); 
     myActivity.connections.clear(); 
     myActivity.connections.addAll(group.getClientList()); 
     if(myActivity.connections.size() == 0) 
     { 
      myActivity.connections.add("No Connected Devices"); 
     } 
     myActivity.adapter.notifyDataSetChanged(); 
    } 

당신이 연결된 장치의 주소를 원하는해야 수신기에서 (구체적 WIFI_P2P_CONNECTION_CHANGED_ACTION 섹션에서), 당신은 단순히 연결을 호출 [색인] .deviceAddress.