У меня был проект, основанный на модуле WiFly, и я успешно разработал приложение, которое выполняет tcp и udp соединения с модулем через сеть Wi-Fi. Нет, мне не нужно было создавать свой телефон в качестве точки доступа. Я могу общаться со своего телефона на WiFly с беспроводным маршрутизатором в качестве точки доступа. Я также мог бы заставить его войти в командный режим, чтобы я мог редактировать его конфигурацию. Если вы все еще ищете надежное решение, я был бы рад предоставить вам мой код.
РЕДАКТИРОВАТЬ: Ниже приведен полный код приложения, которое я сделал. Он был использован для подключения к WiFly в проекте домашней автоматизации.
MainActivity
public class MainActivity extends Activity {
private ToggleButton toggleButton1;
private TcpClient mTcpClient;
private Spinner spinner1, spinner2,spinner3,spinner4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItemsOnSpinner2();
addItemsOnSpinner3();
addItemsOnSpinner4();
new connectTask().execute("");
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
Button button_send = (Button) findViewById(R.id.button_send);
button_send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = String.valueOf(spinner1.getSelectedItem()) + " " + String.valueOf(spinner2.getSelectedItem()) + " " + "ON";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
Button button_send1 = (Button) findViewById(R.id.button_send1);
button_send1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = String.valueOf(spinner1.getSelectedItem()) + " " + String.valueOf(spinner2.getSelectedItem()) + " " + "OFF";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
Button button_intensity = (Button) findViewById(R.id.button_intensity);
button_intensity.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = String.valueOf(spinner1.getSelectedItem()) + " " + String.valueOf(spinner2.getSelectedItem()) + " " + String.valueOf(spinner3.getSelectedItem());
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
Button button_turnon = (Button) findViewById(R.id.button_turnon);
button_turnon.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = String.valueOf(spinner4.getSelectedItem()) + " ON";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
Button button_turnoff = (Button) findViewById(R.id.button_turnoff);
button_turnoff.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = String.valueOf(spinner4.getSelectedItem()) + " OFF";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
toggleButton1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
String message = "$$$";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
} else {
String message = "exit\r";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
}
});
Button button_led1 = (Button) findViewById(R.id.button_led1);
button_led1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = "set sys iofunc 0x1\r";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
Button button_led2 = (Button) findViewById(R.id.button_led2);
button_led2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = "set sys iofunc 0x2\r";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
Button button_led3 = (Button) findViewById(R.id.button_led3);
button_led3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = "set sys iofunc 0x4\r";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
Button button_reset = (Button) findViewById(R.id.button_reset);
button_reset.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = "set sys iofunc 0x0\r";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
Button button_save = (Button) findViewById(R.id.button_save);
button_save.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = "save\r";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
Button button_reboot = (Button) findViewById(R.id.button_reboot);
button_reboot.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = "reboot\r";
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
}
});
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("load1");
list.add("load2");
list.add("load3");
list.add("load4");
list.add("load5");
list.add("load6");
list.add("load7");
list.add("load8");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addItemsOnSpinner3() {
spinner3 = (Spinner) findViewById(R.id.spinner3);
List<String> list = new ArrayList<String>();
list.add("Default");
list.add("10");
list.add("20");
list.add("30");
list.add("40");
list.add("50");
list.add("60");
list.add("70");
list.add("80");
list.add("90");
list.add("100");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(dataAdapter);
}
public void addItemsOnSpinner4() {
spinner4 = (Spinner) findViewById(R.id.spinner4);
List<String> list = new ArrayList<String>();
list.add("s1");
list.add("s2");
list.add("s3");
list.add("s4");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner4.setAdapter(dataAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class connectTask extends AsyncTask<String,String,TcpClient> {
@Override
protected TcpClient doInBackground(String... message) {
//we create a TcpClient object and
mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
@Override
//here the messageReceived method is implemented
public void messageReceived(String message) {
//this method calls the onProgressUpdate
publishProgress(message);
}
});
mTcpClient.run();
return null;
}
}
}
И этот класс создает TCP-клиент для связи с WiFly
package com.example.homauto;
import android.util.Log;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient {
public static final String SERVERIP = "192.168.1.5";
public static final int SERVERPORT = 2000;
// message to send to the server
private String mServerMessage;
// sends message received notifications
private OnMessageReceived mMessageListener = null;
// while this is true, the server will continue running
private boolean mRun = false;
// used to send messages
private PrintWriter mBufferOut;
// used to read messages from the server
private BufferedReader mBufferIn;
/**
* Constructor of the class. OnMessagedReceived listens for the messages received from server
*/
public TcpClient(OnMessageReceived listener) {
mMessageListener = listener;
}
/**
* Sends the message entered by client to the server
*
* @param message text entered by client
*/
public void sendMessage(String message) {
if (mBufferOut != null && !mBufferOut.checkError()) {
mBufferOut.println(message);
mBufferOut.flush();
}
}
/**
* Close the connection and release the members
*/
public void stopClient() {
mRun = false;
if (mBufferOut != null) {
mBufferOut.flush();
mBufferOut.close();
}
mMessageListener = null;
mBufferIn = null;
mBufferOut = null;
mServerMessage = null;
}
public void run() {
mRun = true;
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVERPORT);
try {
//sends the message to the server
mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
//receives the message which the server sends back
mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the server
while (mRun) {
mServerMessage = mBufferIn.readLine();
if (mServerMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(mServerMessage);
}
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
//class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
Надеюсь, это поможет кому-нибудь в будущем. Код применим ко всем tcp-серверам, а не только к WiFly.