Send and receive SMS (Short Message Service), read PhoneBook from GSM card SIM, handle communication with Serial Port (RS-232 hardware) with Java Communication Api :

- Send SMS and Receive SMS / encode and decode binary PDU format
- List, read, write and delete SMS in specific memory  (SIM or Phone)
- List, read, write and delete PhoneBook in specific memory (SIM or Phone)

Accept commands 3GPP 27.007 and recommandations 27.005 GSM.

Needs :

Download and install in your classpath Java Comm API  :
http://java.sun.com/products/javacomm/ (Sun decided to not continue on this way...)
or same spec (LGPL license) :
http://www.rxtx.org/
or here :
javaSerial

Install javax.comm.properties file in classpath of your application :

#
# Drivers loaded by the Java Communications API standard extension
# at initialization time
#
# Format:
#    Each line must contain ONE driver definition only
#    Each line must be of the form:
#            driver=<ClassName>
#        No spaces or tabs in the line.
#        ClassName must implement the interface javax.comm.CommDriver
#            example: driver=Win32Serial
#   
#
# The hash(#) character indicates comment till end of line.
#
# Windows Serial Driver
Driver=com.sun.comm.Win32Driver
# end file

Install win32com.dll for Win32 only in your JRE (for me \j2sdk1.4.x_xx\jre\bin\)
See for linux : http://wass.homelinux.net/howtos/Comm_How-To.shtml

Now we can communicate with SIM via the Serial Port of GSM Phone :
- Use virtual Serial Port BlueTooth Device of you GSM, use a Bluetooth Adapter to communicate with your GSM.
(Pairing communication between PC to GSM before use this program...)
- Or use IRDA with virtual COM port : http://www.ircomm2k.de/
- Or use a cable...


Example :

        // Config serial port parameters!
        SerialParameters params = new SerialParameters();
        params.setPortName("COM6"); // default COM1
        params.setBaudRate(115200); // default 115200
        params.setFlowControlIn(SerialPort.FLOWCONTROL_NONE); // default none flowcontrol
        params.setFlowControlOut(SerialPort.FLOWCONTROL_NONE); // default none flowcontrol
        params.setDatabits(SerialPort.DATABITS_8); // default data bits 8
        params.setStopbits(SerialPort.STOPBITS_1); // default stop bits 1
        params.setParity(SerialPort.PARITY_NONE); // default none parity bits 1
        
        // object sms client
        Sms sms = new Sms(params);
        
        // give time for the modem to warm up
        //try { Thread.sleep(5000); }
        //catch(InterruptedException ie){}
        
        System.out.println("Driver loading... " + sms.initializeWinDrivers());
        try
        {
            //lets use a single SerialComm object to initialize everything
            String[] portArray = sms.listPorts();
            System.out.println("Number of ports detected: " + portArray.length);
            for (int i = 0; i < portArray.length; i++)
            {
                System.out.println("OPEN serial Port : " + portArray[i]);
            }
            System.out.println();
        
            // open connection serial port
            sms.openConnection("COM6");//, "1111"); // COM1, optional pin code
            
            // Send a SMS with short message service center (french smsc by example...)
            // Bouygues Telecom         smsc = 33660003000
            // Orange                   smsc = 33689004000
            // SFR                      smsc = 33609001390

            //sms.SendMessage("3361xxxxxxx", "33660003000", "Testing Network connection");
            //sms.SendMessage("xxxxx", "33660003000", "CONTACT"); // call sms+ french service
            
            // store sms on SIM memory! (SM)
            //sms.WriteTextMessage("336xxxxxxxx", "33660003000", "coucou");
            
            // store sms on SIM memory! (ME) (simulated receive sms unread in test!)
            //sms.WriteTextUnReadMessage("336xxxxxxxx", "33660003000", "test1");
            
            // last message status ?
            //if (sms.getPortStatus() == ERROR) System.out.println("----> Error : SMS not SEND !");
            //else if (sms.getPortStatus() == OK) System.out.println("----> Ok : SMS SEND - check folder on SIM!");
            
            // init write memory sms "\"ME\",\"SM\",\"MT\"
            boolean initMemSms =  sms.initializeSmsMemory("\"SM\"");
            System.out.println("Init Sms Memory  : "+initMemSms);
                
            // get size sms storage
            int len_sms = sms.getSizeListSMS();
            System.out.println("Size stored SMS  : "+len_sms);
        
            // start index is 1 !?
            int index_sms = sms.getIndexMemory();
            for(int i=index_sms; i<=len_sms; i++)
            {
                // read sms
                sms.ReadSMS(i);

                // test if last message is a error  
                if (sms.getPortStatusMsg().startsWith("+CMS ERROR")) break;
                    
                // get incoming sms
                IncomingSms in_sms = sms.getRxMS();

                if (in_sms != null)
                {
                    System.out.println("\nSMS received : "+in_sms.toString());
                    //sms.DeleteSMS(i);
                }
                else System.out.println("\nProblem SMS received !!!");
            }
            
            // Get current memory used for book!
            String memoryBook = sms.getPhoneBookMemory();
            System.out.println("PhoneBook memory  : "+memoryBook);
            
            boolean initMemBook =  sms.initializePhoneBookMemory("\"SM\"");
            System.out.println("Init PhoneBook Memory  : "+initMemBook);
            
            int len_book = sms.getSizeListPhoneBook();
            System.out.println("Size stored PhoneBook : "+len_book);
                        
            // get index of current memory
            int index_book = sms.getIndexMemory();
            String book_old = "";
            for(int i=index_book; i<=len_book; i++)
            {
                 // read phone book
                 String book = sms.getPhoneBook(i);
                
                 if (book_old.equals(book)) break; // same response each time!
                else book_old = book;
                
                System.out.println("Phone Book : '"+book+"'");
            }
            
            // index , phone number, type, name
            //sms.WritePhoneBookMessage(8, "0xxxxxxxx", 129, "test one");
            //sms.DeletePhoneBook(8);

        }
        catch (IOException e)
        {
            System.err.println("Communication problem :" + e);
            e.printStackTrace();
        }
        finally
        {
            // close connection to serial port / modem
            try { sms.close(); } catch (NullPointerException ex) {}
        }


Source : Sms.java (see other attachments)