how to connect esp8266 softAp with android app - java

I want some brief idea/links for reference to start how to connect esp8266 router/access point with an android app.In esp8266 static ip is 192.168.4.1 wants to control led blink or other feautre with an android app .
how to make establish connection between esp8266 and android app .

On Android side it's just network communication without any features. Take a look at Official Documentation and tutorials like this. Everything depends on esp8266 firmware:
if it implements HTTP web server You can use HttpUrlConnection and GET or POST requests on Android side and corresponding script on esp8266 side;
if it implements ServerSocket You can use Socket connection an implement Socket Client on Android side.
Update:
Socket communication with esp8266 You should do it in separate (not UI) thread. Full example is something like that:
class SocketClientThread implements Runnable {
DataInputStream dis;
DataOutputStream dos;
String strResponseData;
#Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName("<address>");
clientSocket = new Socket(serverAddr, <port_number - 80 in your example>);
dos = new DataOutputStream(clientSocket.getOutputStream());
dis = new DataInputStream(clientSocket.getInputStream());
// now you can write data to stream
dos.writeUTF("Hello");
// you can also read data from stream
strResponseData = dis.readUTF();
} catch (UnknownHostException ignore) {
} catch (IOException ignore) {
}
finally{
if (clientSocket != null){
try {
clientSocket.close();
}
catch (IOException ignore) {
}
}
}
}
}
And than You can use SocketClientThread this way:
Thread socketClientThread;
socketClientThread = new Thread(new SocketClientThread());
socketClientThread.start();

Related

Connection between Android Server and Laptop Client using same wifi

I want to connect android server application with laptop client java which connected on same wifi network.
i try the following code.
laptop client code
String IPToConnect="192.168.0.";
int i=0;
Socket skt;
PrintWriter pw;
try
{
for(i=105;i<=199;i++)
{
try
{
System.out.println(i);
skt=new Socket(IPToConnect+i,4444);
pw=new PrintWriter(skt.getOutputStream(),true);
pw.write("KP");
pw.flush();
pw.close();
skt.close();
}
catch(Exception ex)
{
continue;
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
and android server code
private static ServerSocket serverSocket;
private static Socket client;
private static InputStreamReader inputStreamReader;
private static BufferedReader br;
private static String message;
serverSocket = new ServerSocket(4444)
try
{
client = serverSocket.accept();
inputStreamReader = new InputStreamReader(client.getInputStream());
br = new BufferedReader(inputStreamReader);
message = br.readLine();
System.out.println(""+message);
inputStreamReader.close();
client.close();
}
catch(Exception ex)
{
System.out.println("HELLO ERROR");
}
above code working fine for me
i want the program working like this
Android mobile app and laptop connected through same wifi network
when android app start it display the laptop on list when laptop start that client application
above code working fine but it may be not work when different router use different ip addresses means not start with(192.168.0) and it send request to all device means it time consuming
so please help me to solve the problem, in short i want to list laptop on android app when laptop start it client app
thank you

Bluetooth Server Android - Client Java Bluecove. UUID?

I'm writing an application to communicate between my smartphone and a computer using a bluetooth device.
I'm using Bluecove to manage the bluetooth on the computer, and the android API for my android device.
However, when I'm debugging, nothing happens. I think that the problem is that the UUID is wrong. I'm not sure how to get the devices to identify each other, in order to establish a connection.
I have read some other "questions" about those tags, but what I've tried didn't fix my problem:
Simple Bluetooth data receiver Android
Android: obtaining uuid of a bluetooth device
Etc...
Here's what I've written so far:
For tho android (Server) (This is the function that will make the connection)
public void connectSocket(){
blueAdapter.cancelDiscovery(); // Cancel discovery because it'll slow down the connection
final BluetoothServerSocket serverSocket;
BluetoothServerSocket sSocket= null;
try{
sSocket = blueAdapter.listenUsingRfcommWithServiceRecord("BluetoothJANE", MY_UUID);
}catch(IOException e){}
serverSocket = sSocket;
Thread acceptThread = new Thread(new Runnable() {
#Override
public void run() {
BluetoothSocket socket = null;
while(true){
try{
socket = serverSocket.accept();
}catch(IOException e){
break;
}
if(socket != null){
try{
iStream = socket.getInputStream();
oStream = socket.getOutputStream();
} catch(IOException e){}
}
}
}
});
acceptThread.start();
}
For java app on PC (This is the constructor of the class (it's on an independent thread) that will manage the bluetooth connection)
public ModuleBluetooth() {
StreamConnectionNotifier notifier = null;
StreamConnection connection = null;
try {
blueDevice = LocalDevice.getLocalDevice();
blueDevice.setDiscoverable(DiscoveryAgent.GIAC);
String url = "btspp://localhost:" + MY_UUID.toString()
+ ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier) Connector.open(url);
} catch (BluetoothStateException e) {
System.out
.println("ModuleBluetooth: Error getting the bluetooth device");
} catch (IOException e) {
}
System.out.println("waiting for connection...");
try {
connection = notifier.acceptAndOpen();
System.out.println("Conenction created");
} catch (IOException e) {
System.out.println("Can not create the connection");
}
}
Could somebody please help me? Any thoughts would be very much appreciated.
I have also tried to use some functions to acquire the UUID (in android) such as, [fetchUuidsWithSdp][2] (and the related functions) but eclipse doesn't recognize that functions (It seems that they don't exist in "my" API).
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#fetchUuidsWithSdp%28%29
Try this example, http://luugiathuy.com/2011/02/android-java-bluetooth/ . I also had problem related to UUID, in this example, Converting UUID to 00001101-0000-1000-8000-00805F9B34FB worked out of the box. See this link: http://www.avetana-gmbh.de/avetana-gmbh/produkte/doc/javax/bluetooth/UUID.html

Threads & networking issue

Given the following code:
Client c1 = new Client();
c1.connect("127.0.0.1",1300);
Connect function:
public void connect(String serverName, int port)
{
try {
Socket socket = new Socket(serverName,port);
connection = new ConnectionProxy(socket);
connection.start();
}
catch(IOException e)
{
e.printStackTrace();
}
}
(ConnectionProxy class extends Thread) :
public class ConnectionProxy extends Thread {
private Socket socket;
private InputStream is;
private OutputStream os;
private StringConsumer client;
public ConnectionProxy(Socket socket)
{
this.socket = socket;
try {
is = socket.getInputStream();
os = socket.getOutputStream();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void run () {
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
while (socket != null)
{
try {
String msg = dis.readUTF();
System.out.println(msg);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
I'm trying to implement a chat and I'm finding it difficult to send a message written by a client to all of the currently connected clients.
How could I do that? Should I hold the reference for each object (like c1) on the server side, or should I hold that ConnectionProxy thread on the server side?
If not, how do I implement that correctly and efficiently?
Would love to get some help!
Thanks!
Without being given much code, I'll outline what you'd want to do to achieve your goal.
On your server:
Keep an array or something similar of all connected client objects
Implement a send() function in your client class
Implement a broadcast() function that loops through the client list and sends each of them the message (using the aforementioned send() function
Make sure to keep track of (and remove) any dead/disconnected clients from your list, otherwise you'll run into trouble trying to send to them.
On your client:
Make sure you send a "connection terminated" message when you close/disconnect to tell the server you're leaving (makes it easier for the server to remove you)
The server should create a new client handler thread for each incoming connection.
For example, on the server side try something like:
ServerSocket server = new ServerSocket(port);
while (true) {
Socket client = server.accept();
//add incoming client to connected clients vector.
HandleClient c = new HandleClient(client);
clients.add(c);
}
After creating and storing clients in your vector of clients, you can the implement on the HandleClient class run() method a bufferReader (again not a must) to get your client text

socket connection works good on emulator but can't send in real device(j2me)

I am developing a server/client application .The application works fine on an emulator but when I test it on a Nokia 5200 or a Nokia 6303i, although the connection establishes successfully, the server blocks on first read method. In other words, the client(j2me) application can't send data to server.
My client part:
Thread occ=new Thread(new Runnable() {
public void run() {
try {
SocketConnection sc = (SocketConnection)Connector.open("socket://213.233.169.142:2000");
sc.setSocketOption(SocketConnection.DELAY, 0);
OutputStream os=sc.openDataOutputStream();
DataOutputStream dos=new DataOutputStream(os);
InputStream is=sc.openDataInputStream();
DataInputStream dis=new DataInputStream(is);
//dos.writeUTF(receiverT.getString());
os.write("saalam".getBytes());
os.flush();
dos.writeUTF(Midlet.userPhoneNumber);
dos.flush();
dos.writeUTF(messT.getString());
dos.flush();
while((!dis.readUTF().equals("system-use:code=2")) && false)
{
}
dos.close();
os.close();
sc.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
occ.start();
My server part:
serverSocket = new ServerSocket(2000);
Socket socket=serverSocket.accept();
System.out.println("connection stablished");
inp=new DataInputStream(mySocket.getInputStream());
outp=new DataOutputStream(mySocket.getOutputStream());
receiverTemp=inp.read();//the server code blocks on this line
senderTemp=inp.readUTF();
.
.
.
.
Did you set socket permissions in the JAD file? This fact is very important to solve this kind of issues in devices
The problem finally solved.the problem was that some mobile operators don't allow send/receive information with raw sockets so we used HTTP sockets on port 80 and it worked.

Simple Client Server communication

I am learning currently about client server communication using Java through sockets.
First of all I retrieve my own machine's IP Address using following code.
InetAddress ownIP=InetAddress.getLocalHost();
//the result being 192.168.56.1
Now I write the simple client server application using the above mentioned address as follow
public class SimpleClientServer {
public static void main(String[] args)
{
//sending "Hello World" to the server
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try
{
clientSocket = new Socket("192.168.56.1", 16000);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
out.println("Hello World");
out.close();
in.close();
clientSocket.close();
}
catch(IOException e)
{
System.err.println("Error occured " + e);
}
}
}
The result hower reads a follow.
Error occured java.net.ConnectException: Connection refused: connect
What is the reason for this. Is it just the wrong host address?
From the code you have given you seem to suggest that there is currently nothing listening on port 16000 for the socket to connect to.
If this is the case you need to implement something like
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(16000);
}
catch (IOException e) {
System.err.println("Could not listen on port: 16000.");
System.exit(1);
}
More information can be found in the Java online documentation and a full example is included.
With sockets, no matter what language you're using, you either initiate a connection with socket_connect or you listen and accept with socket_listen and socket_accept. Your socket_connect call is trying to connect to an ip address that doesn't seem to be listening to anything.

Categories