how to work with sockets, activities and views? - android

I'm new to programming in android and I need help to build an application. I want two phones to connect to each other, one being the client and the other being the server. I want the client to have 2 stages. In the first the user would input the ip of the server y click on a button to stablish the connection. In the second one the user would input a message and click on a button to send it to the server.
The code shown bellow is to send the string "message" to the server but as I was saying I want the user to be able to input the string. I don't know how to tackle his issue, do I need a second activity to be called once the connection is established in the activity I show bellow? In that case I wouldn't know how to pass a socket to another activity, I only know how to pass strings. Besides, I would need another button and therefore a new OnClickListener and I would still need to pass the socket to that function.
Without using a second activity I don't know how to make the second input field (the one where the user would input the message to send to the server) to show up once the connection is established. The views (layout.xml) for this activity are already associated to the same, I can't just clear the screen and create a new EditText field on the fly.
I hope I made myself clear.
Thanks in advance
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
serverIp = (EditText) findViewById(R.id.server_ip);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(connectListener);
}
private OnClickListener connectListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
}
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
connected = true;
while (connected) {
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
**out.println("messageToSend");**
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
}

I don't think , you need a second activity to send message over socket.
Anyways , It depends on you design --
you can have two editTexts at a same but their visibility would be different.
EditText serverIpAddressET;
EditText messageET ; // Deafult visibility Gone
serverIpAddressET can be visible when user has not provided socket IP.
Once connection is established , you can change the visibility of serverIpAddressET to the Gone and make Visible the messageET .

Well a quick fix would be to hide the currently Displayed EditText and Button via
view.setVisiblility(View.GONE)
and display the other editText and button via
view.setVisibility(View.Visible)
and use same activity...
But a better solution would be to open the socket for communication in a service look in to android Service with Sockets... start the service in first activity and when connection is established shift too second activity and bind the activity with service so there can be communication btw activity and service...

Not entirely sure what you're looking for here but if I understand correctly you are woundering how to send a message. Simply add
message2send= (EditText) findViewById(R.id.user_message);
and then where you have
**out.println("messageToSend");**
replace with
**out.println(message2send);**
and add an extra edit text field to your layout.
It should be noted that the way you've gone about this is not ideal as the other phone (server) would have to be listening to the socket in order to receive the message which would cause additional data charges as well as wasting battery power (traditonal in mobile a server is between the 2 phones to store the message in case user if offline or phone is dead)
Hope this helps non the less!

Related

Can't able to receive group chat messages using smack-android:4.1.4

I am developing chat app using smack library. I have an issue in group chat. In my app, i am creating a group and in that members are auto-joined.i want to notify all user when I send a message in the group even if they had not initiated a chat.My code is as follow in that I have place listener in init method but unable to receive a message.
multiUserChatManager = MultiUserChatManager.getInstanceFor(mConnection);
mMultiUserChat = multiUserChatManager.getMultiUserChat(to);
mConnection.addAsyncStanzaListener(this, null);
DiscussionHistory history = new DiscussionHistory();
history.setMaxStanzas(0);
mMultiUserChat.addMessageListener(this);
mConnection.addSyncStanzaListener(this, null);
try {
mMultiUserChat.join(from, "", history, SmackConfiguration.getDefaultPacketReplyTimeout());
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
Here is message listener of group
#Override
public void processMessage(Message message) {
Logg.e(TAG,"Message received group..");
}
I don't know why this method does not call when someone send message in group, even I joined group, If I create 1 group and joined 2 users, when 1 user sends message in group then user2 can't able to receive message, but when user 2 send message inside this group then they both are able to receive messages.
Please help me, I can't able to find the solution. Please don't give suggestion which is already deprecated.
Thanks in Advance.!!
I'm full editing answer after full code review. -again-
I suggest to refactor your code to keep separation of roles in more than 1 huge class.
Basically you are especting messages in wrong listener due to many "addasync - addsync" in your code and you are able to receive messages just as side effect of your monster-class-all-in!
I see many optimization you need to apply to your code.
It's too long to explain and out of the question, however, just as example:
1. sendGroupMessage You can check by MultiUserChatManager if you
already joined the chat and then send the message. You must fire a
"join" just once, not everytime you want to send a message.
2. mMultiUserChat.addMessageListener(this);
A listener must be added ONCE or you'll create tons of threads. Probably it works because you have a singleton. While you have a listener, you don't need to add it anymore to that chat if you don't remove it.
mConnection.addSyncStanzaListener(this, null);
Be carefull: you are adding your listener (wich one? You implements tons of listeners with same class) to your connection. Before or later your code will eat an important stanza (prolly a custom IQ) and you'll have an hard to discovery side effects.
mConnection.addAsyncStanzaListener(this, null); same of 3
Check for ProviderManager.addExtensionProvider(), before or later
you'll need some.
Hope that helps.
Try This
step1 : 1 remove this
mConnection.addAsyncStanzaListener(this, null);
mConnection.addSyncStanzaListener(this, null);
Step 2 : add this
private StanzaTypeFilter serverFilter;
private StanzaListener stanzaListener = null;
private XMPPTCPConnection mConnection;
registerStanzaListener(); // where you init connection
public void registerStanzaListener() {
serverFilter = new StanzaTypeFilter(Message.class);
if (stanzaListener != null) {
mConnection.removeAsyncStanzaListener(stanzaListener);
}
stanzaListener = new StanzaListener() {
#Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
processMessage((Message) packet);
}
};
mConnection.addAsyncStanzaListener(stanzaListener, serverFilter);
}
}

Best practice for closing websocket in android

I am using this library for connecting to a websocket server from android.
Specifically this part :
AsyncHttpClient.getDefaultInstance().websocket("ws://192.168.2.10:8000/temp" , "my-protocol", new WebSocketConnectCallback() {
#Override
public void onCompleted(Exception ex, WebSocket webSocket) {
if (ex != null) {
ex.printStackTrace();
return;
}
webSocket.send("a string");
webSocket.setStringCallback(new StringCallback() {
#Override
public void onStringAvailable(String s) {
Debug.Log( LOGTAG ,"I got a string: " + s);
}
});
webSocket.close(); // issue here
}
});
I would like to close the socket when I click a button. Now everytime I want to send a message to the socket I open it and close it.
I would like to open it once and keep it alive and close it when I click a close button. My idea was to pass a variable to the WebSocketConnectCallback and make a static variable and based on this variable close the socket.
I would like to know what is the best practice in a situation like this.
Use the Application class (http://developer.android.com/reference/android/app/Application.html):
Inherid your own class for Application and here you can track the socket, open it, close it as you need.
See a tutorial (first google match, maybe there is a better one): http://www.intertech.com/Blog/androids-application-class/
So basically extend Application and add your class in the manifest file as application class.
Your may add a timer that might close the socket after several time while not used.

Will a Slave device need a different application than the Master?

All Bluetooth projects I have previously done, had an Android device acting as the Master, with a Bluetooth dongle or chip acting as a single Slave. The project I am working on will have an Android Tablet acting as the Master, then have several other Android Devices acting as the Slaves.
What is the best practice in this situation? Should I write two separate applications? One for the master, another for the slaves? Or would it be plausible to use one application for both rolls?
The best way to do this would be to just make one application and then just have different code logic based on if the device is a master or a slave.
Instead of doing just a simple SPP connection, you could create a sort of bluetooth "Server" and wait until x number of devices connect to the server and then do whatever it is you want to do in your app.
An example (From the developer guide: Here)
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
// Use a temporary object that is later assigned to mmServerSocket,
// because mmServerSocket is final
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) { }
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(socket);
//in your case you wouldn't want to close the server socket since you want to
//connect more than one device. So keep listening until you get all the devices.
//you're also going to have to use different UUID's for each new device.
// mmServerSocket.close();
break;
}
}
}
/** Will cancel the listening socket, and cause the thread to finish */
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) { }
}
}
In this code the server is closed as soon as one connection is made, in your case you would just keep listening for more sockets until you get all the sockets. You're going to need multiple threads to handle all the connections and some synchronization for handling all the data from all the connections depending on what you intend on doing.

android: communication with PC desktop app and remote mediaplayer controller

Dont get angry on me please. I have two questions, I think on very similar theme, so I decided to merge them into one. I have my app on android that is using sensors to do some calculations. I am storing there sesults in my database. What i want to do is to send my data from my phone to my desktop app also with a database (on button click).
To be more precise, here is an example: My light sensor reads current light intensity. Lets say it is 1000lux. Now, when I click my button "Send" in my android app, it will send this value to my desktop apps database. That desktop app will read that value and will show it to user.
Is it possible via WIFI? or better via web, so i will not be limited with distance?
How can android manage this kind of communication?
And my second question is, if controlling media player on my PC is similar to what i said.
EDIT:
I did some research and found one Socket tutorial. I tried it exactly like it is there. So i have this in my android app:
public class Client extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
Client myCli = new Client();
try {
myCli.run();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_client, menu);
return true;
}
public void run() throws Exception {
Socket mySkt = new Socket("192.168.1.6", 9999);
PrintStream myPS = new PrintStream(mySkt.getOutputStream());
myPS.println("Hello server");
BufferedReader myBR = new BufferedReader
(new InputStreamReader(mySkt.getInputStream()));
}
}
and this in netBeans:
//Author: WarLordTech
//Website: http://www.youtube.com/user/WarLordTech
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception{
Server myServ = new Server();
myServ.run();
}
public void run() throws Exception{
ServerSocket mySS = new ServerSocket(9999);
Socket SS_accept = mySS.accept();
BufferedReader SS_BF= new BufferedReader(new InputStreamReader
(SS_accept.getInputStream()));
String temp = SS_BF.readLine();
System.out.println(temp);
if (temp!=null) {
PrintStream SSPS = new PrintStream(SS_accept.getOutputStream());
SSPS.println("Got something");
}
}
}
It still isnt workiong. Do I have to set up my network somehow?
You could do it using TCP Sockets. Many languages have implementations for Socket programming so you would "need" to program your desktop app in Java (of course that is always possible!).
Socket Tutorial in Java
This would work over the net and local wifi. You could use other methods for local wifi such as a UDP connection. You'd need to setup a TCP server and make sure you had access etc.
There may be other ways to do this but it's not such a trivial task to do!

How to programmatically pair a bluetooth device on Android

For my application I'm trying to programmatically pair a bluetooth device. I'm able to show the pairing dialog for the device I want to pair and I can enter a pincode. When I press "Pair" the dialog is removed and nothing happens.
I only need to support devices with Android 2.0 and newer.
Currently I am using the following code to start the pairing progress:
public void pairDevice(BluetoothDevice device) {
String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
Intent intent = new Intent(ACTION_PAIRING_REQUEST);
String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
intent.putExtra(EXTRA_DEVICE, device);
String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
int PAIRING_VARIANT_PIN = 0;
intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
Before starting a pairing request I stop scanning for new devices.
My application has the following bluetooth permissions:
android.permission.BLUETOOTH_ADMIN
android.permission.BLUETOOTH
I managed to auto request a pairing procedure with keyboard featured devices through an app working as a service checking the presence of a specific kind of device and a modified version of the Settings app.
I have to say that I was working on a custom device running Android 4.0.3 without external controls (no back/Home/confirm buttons): pairing a controller on boot complete without any interaction until PIN request was mandatory.
First I created a service starting an activity on boot (with android.intent.action.BOOT_COMPLETED and android.permission.RECEIVE_BOOT_COMPLETED) that checks periodically the presence of a 1344 class device (a keyboard, the only way to input data on request) on the onReceive callback:
public void onReceive(Context context, Intent intent)
...
BluetoothDevice dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
...
if(dev.getBluetoothClass().getDeviceClass() == 1344){...}
Once filtered I choose the first keyboard available and then I pass the BT address to the Settings app:
Intent btSettingsIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
btSettingsIntent.putExtra("btcontroller", dev.getAddress());
startActivityForResult(btSettingsIntent, 1);
The tricky part was looking for the best position to call the pairing process. Using only the
intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
led me to a paring dialog that once closed left me with the device paired, but unusable.
Digging into the classes of com.Android.settings.Bluetooth I found my way through the
createDevicePreference(CachedBluetoothDevice cachedDevice)
in the DeviceListPreferenceFragment.
From there I did compare my previously selected BT address with those available coming up and once successfully matched I call
cachedDevice.startPairing();
I know, it's tricky and requires access to the Android source code, but in a custom environment it works.
I hope this could be helpful.
It's my answer:
in onCreate() write this:
registerReceiver(incomingPairRequestReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));
then create variable
private final BroadcastReceiver incomingPairRequestReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
BluetoothDevice dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//pair from device: dev.getName()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
dev.setPairingConfirmation(true);
//successfull pairing
} else {
//impossible to automatically perform pairing,
//your Android version is below KITKAT
}
}
}
};
Unfortunately, I think the best that you are going to get is opening up Settings/Wireless & networks/Bluetooth Settings for the user like so:
Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivityForResult(intent, REQUEST_PAIR_DEVICE);
Using reflection you can call the method createBond from the BluetoothDevice class.
See this post: How to unpair or delete paired bluetooth device programmatically on android?
There is also a solution for unpair.
Reflection is DODGY, different manufacturers can change these underlying methods as they wish! I have tested many different apps on our 10 devices here and these reflection method only works fully on roughly 75% of devices. If you want an app that works for everyone be very careful when using reflection - try some cloud testing to test your app on 100+ devices and check the failure rate.
In this case reflection is not needed at all since API 19 (KitKat 4.4)
BluetoothDevice has new method CreateBond.
private void pairDevice(BluetoothDevice device) {
device.createBond();
}
developer.android.com/reference/android/bluetooth/BluetoothDevice.html
May be you need to startActivityForResult instead of only startActivity?
Other option is to look into the BluetoothChat application sample and start an RFComm connection socket, as soon as you start the socket a pairing request will automatically appear without needing to send a separate intent for pairing. This way you won't need to handle pairing.
http://developer.android.com/resources/samples/BluetoothChat/index.html
I am using this class to do connection between my client smartphone and the server device:
private class ConnectThread extends Thread
{
private final BluetoothSocket mmSocket;
private final UUID WELL_KNOWN_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
public ConnectThread(BluetoothDevice device)
{
// Use a temporary object that is later assigned to mmSocket,because
// mmSocket is final
BluetoothSocket tmp = null;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try
{
tmp = device.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
//This is the trick
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (Exception e)
{
e.printStackTrace();
}
mmSocket = tmp;
}
public void run()
{
DebugLog.i(TAG, "Trying to connect...");
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try
{
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
DebugLog.i(TAG, "Connection stablished");
} catch (IOException connectException)
{
// Unable to connect; close the socket and get out
DebugLog.e(TAG, "Fail to connect!", connectException);
try
{
mmSocket.close();
} catch (IOException closeException)
{
DebugLog.e(TAG, "Fail to close connection", closeException);
}
return;
}
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel()
{
try
{
mmSocket.close();
} catch (IOException e)
{
}
}
}
First, get the BluetoothDevice object that you want to connect (listing paired devices or discoverying devices). Then do:
ConnectThread ct = new ConnectThread(device);
ct.start();
Because connect() is a blocking call, this connection procedure should always be performed in a thread separate from the main activity thread. See Android Developers for more detailed info.
I've found that using different values for PAIRING_VARIANT_PIN result in different pairing UI behaviours.
See this page:
http://code.google.com/p/backport-android-bluetooth/source/browse/trunk/backport-android-bluetooth201/src/backport/android/bluetooth/BluetoothDevice.java?spec=svn67&r=67
I suspect the problem you're having is that both devices are Bluetooth 2.1, in which case a pairing request should result in a 6 digit passkey being displayed on both devices.
The best result I was able to achieve was using PAIRING_VARIANT_PIN = 0. When prompted by my application, I entered pin 1234 and a 6 digit passkey appeared on my target device. The pairing UI finished and that was that.
Either you need to find out how to initiate a Bluetooth 2.1 pairing request, using some other pairing variant or pairing variant pin. Or, you're not catching the result of the activity that's running properly.
Given the amount of time I've been trying to do this, I've decided that my end users will just have to pair using the android settings before using my application.
This is how I get it:
Bluetooth device = mBtAdapter.getRemoteDevice(address);
//address:11:23:FF:cc:22
Method m = device.getClass()
.getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null); // send pairing dialog request
After pairing//
connectDevice(address);
in addition to my comment, by the way, even if these ACTION types did exist, that's not how you use them. here's an example:
Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
intent.putExtra(EXTRA_DEVICE, device);
int PAIRING_VARIANT_PIN = 272;
intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
sendBroadcast(intent);

Categories

Resources