Hello, Below, you will find the source code for a simple application testing access to the rild socket in dev/socket.
Before running the application, I manually changed permissions on rild in dev/socket to allow r/w/x for all users. The application below is based in large part on code I found at http://all4dev.blogspot.com/2009/02/android-localsocket-localserversocket.html As originally written, the application works. When I changed it to use rild socket as the conduit for communication between receiver and sender, it stopped working. Specifically, I believe that connection to the rild socket (s.connect ()) never completes. No exceptions are produced either. I would greatly appreciate it if anyone could let me know where they think I made a mistake, why the application does not work, and what I should do to fix it. Thanks. Alex Donnini package com.android.sockettest; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.net.LocalServerSocket; import android.net.LocalSocket; import android.net.LocalSocketAddress; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; /** * * @author Denis Migol * */ public class SocketTest extends Activity { private static String TAG = "sockettest"; LocalSocket mServer; static final String SOCKET_NAME_RIL = "rild"; static final int SOCKET_OPEN_RETRY_MILLIS = 4 * 1000; LocalSocketAddress mLocalSocketAdress = new LocalSocketAddress(SOCKET_NAME_RIL, LocalSocketAddress.Namespace.RESERVED); // background threads use this Handler to post messages to // the main application thread private final Handler handler = new Handler(); public class NotificationRunnable implements Runnable { private String message = null; public void run() { if (message != null && message.length() > 0) { showNotification(message); } } /** * @param message the message to set */ public void setMessage(String message) { this.message = message; } } // post this to the Handler when the background thread notifies private final NotificationRunnable notificationRunnable = new NotificationRunnable(); public void showNotification(String message) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); } class SocketListener extends Thread { private Handler handler = null; private NotificationRunnable runnable = null; public SocketListener(Handler handler, NotificationRunnable runnable) { this.handler = handler; this.runnable = runnable; this.handler.post(this.runnable); } /** * Show UI notification. * @param message */ private void showMessage(String message) { this.runnable.setMessage(message); this.handler.post(this.runnable); } @Override public void run() { showMessage("DEMO: SocketListener started!"); try { while (true) { Log.i(TAG, "running socket listener --- "); LocalSocket s = null; LocalSocketAddress l; try { s = new LocalSocket(); l = new LocalSocketAddress(SOCKET_NAME_RIL, LocalSocketAddress.Namespace.RESERVED); s.connect(l); Log.i(TAG, "Successfully connected to socket s - "+s.toString()); } catch (Exception ex){ try { if (s != null) { Log.i(TAG, "About to close socket after Exception --- "); s.close(); } } catch (Exception ex2) { Log.i(TAG, "Failed to close socket s"); } } mServer = s; Log.i(TAG, "Connected to mServer --- "); if (mServer != null) { Log.i(TAG, "mServer is not null"); InputStream input = mServer.getInputStream(); // simply for java.util.ArrayList int readed = input.read(); int size = 0; int capacity = 0; byte[] bytes = new byte[capacity]; // reading while (readed != -1) { // java.util.ArrayList.Add(E e); capacity = (capacity * 3)/2 + 1; //bytes = Arrays.copyOf(bytes, capacity); byte[] copy = new byte[capacity]; System.arraycopy(bytes, 0, copy, 0, bytes.length); bytes = copy; bytes[size++] = (byte)readed; // read next byte readed = input.read(); Log.i(TAG, "readed - "+readed); } showMessage(new String(bytes, 0, size)); } } } catch (IOException e) { e.printStackTrace(); Log.e(getClass().getName(), e.getMessage()); } } } public void writeSocket(String message) throws IOException { LocalSocket sender; sender = mServer; if (sender == null) { Log.i(TAG, "sender - "+sender); return; } sender.getOutputStream().write(message.getBytes()); sender.getOutputStream().close(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new SocketListener(this.handler, this.notificationRunnable).start(); Button send1 = (Button)findViewById(R.id.send_1_button); send1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { writeSocket("hello"); } catch (IOException e) { e.printStackTrace(); Log.e(getClass().getName(), e.getMessage()); } } }); } } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---