Java – WifiDirectActivity sample modification: Transfer an ArrayList p2p

WifiDirectActivity sample modification: Transfer an ArrayList p2p… here is a solution to the problem.

WifiDirectActivity sample modification: Transfer an ArrayList p2p

I looked at the Android wifi p2p API here and looked at the sample code provided in “WiFiDirectActivity”, which only allows the phone to transfer image files from one phone to another. The code they use for this is:

public void onClick(View v) 
{
     Allow user to pick an image from Gallery or other
     registered apps
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
}

I’ve changed it to the following, no compiler error :

public void onClick(View v) 
{
     Allow user to pick an image from Gallery or other
     registered apps
    ArrayList<String> deck = new ArrayList<String>();
    deck.add("Lulz");
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("ArrayList<String>");
    startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
}

The problem is that this transfer is just one way, it only transfers files, and I want to implement it into my pvp card game app code to transfer the ArrayList object in two ways. What should I do?

Also, how do I transfer the current “ArrayList deck” I instantiated there? I don’t need to search, I’ll know the name of my ArrayList from the code.

Solution

I won’t go out of my way to write a solution for you, you may have already found a solution to the problem. But just in case you haven’t:

The link above seems to cover your ArrayList with socket issues. One comment suggested that it was best to send the raw string and then create an array on the other end. Wi-Fi Direct’s bandwidth should handle this easily, but it should also handle ArrayList transfers.

Regarding two-way connections, I’ll link you to This, which certainly got me started with two ways on Android: sockets, input and output streams, etc. It is recommended to also read the sockets in the Oracles documentation

Wi-Fi Direct is still in its infancy. Good luck.

Related Problems and Solutions