Can't access/read SIM phonebook in Nokia

1.8k views Asked by At

am trying to access both the Phone's phonebook and SIM phonenook on Nokia 5130c-2 XpressMusic. The app runs without errors but it only returns the numbers from the phone's Phonebook. When I list the available phonebooks using this code

String[] all_contact_lists=PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);

it gives me both the Phonebook and SIM card lists. i.e 1. Phone 2. SIM

I have tried explicitly reading from the SIM card using this code but it still returns nothing(even though I have numbers saved in the SIM card.) Code:

ContactList clist = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY,
                "SIM");

Here's my complete code::

import javax.microedition.midlet.*;
import javax.microedition.pim.*;
import com.sun.lwuit.*;
import java.util.*;

public class contacts extends MIDlet
{
    private List my_list=new List();
    private String[] names=null;
    public void startApp()
    {
        Display.init(this);
       Form my_form=new Form("Contacts List");       
       String[] all_contact_lists=PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
       //Iterate through available phonebooks
       for(int db=0; db<all_contact_lists.length; db++)
       {
        try {
            ContactList clist = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY,
                    all_contact_lists[db]);
            Enumeration contacts=clist.items();
            while(contacts.hasMoreElements())
            {
                Contact contact=(Contact)contacts.nextElement();
                try{
                    String phone_contact="";
                    names=contact.getStringArray(Contact.NAME, 0);
                    for(int i=0; i<names.length; i++)
                    {
                        if(names[i]!=null)
                            phone_contact+=" "+names[i];
                    }
                    //my_list.addItem(phone_contact);
                    int phone_numbers=contact.countValues(Contact.TEL);
                    if(phone_numbers>0)
                    {
                         String number=contact.getString(Contact.TEL,0);
                        my_list.addItem(phone_contact+":"+number);
                    }
                    else
                    {
                        my_list.addItem(phone_contact);
                    }
                    //clist.removeContact(contact);
                }
                catch (Throwable t) {
            t.printStackTrace();
            }

            }
        } catch (PIMException ex) {
            ex.printStackTrace();
        }
       }
       //my_list.addItem(all_contact_lists);
       my_list.setRenderingPrototype("WWWWWWWWWWWWWWWWWWWW");
       my_form.addComponent(my_list);
       my_form.show();
       }
    public void pauseApp(){}
    public void destroyApp(boolean unconditional){}


}
2

There are 2 answers

2
Vimal On

An excerpt from PIM javadoc

PIMItems reference its data through fields. A field is a grouping of data values that all have similar characteristics. An example of a field is TEL, which indicates data values for that particular field are telephone numbers. Classes implementing the PIMItem interface defines the possible fields that for that specific class (e.g TEL is defined in the Contact interface as a field that a contact may support).

PIM implementations are not required to support all of the possible fields defined in the classes implementing the PIMItem interface. This is because no native PIM databases contain all of the fields defined in this API. The PIMList that a PIMItem belongs to determines what fields a PIMItem can support and store (all PIMItems in a particular PIMList support the same set of fields). The PIMList.getSupportedFields() method from a particular PIMItem's PIMList is used to find out what fields are supported within this item. Since not all possible fields are actually supported in a particular PIMItem, all fields should be checked for support in the item's PIMList using PIMList.isSupportedField(int) prior to being used in any retrieval or storage method.

Each field has the following pieces of information available for it:

  • Zero or more data values associated with the Field
  • Attributes for data values for the Field
  • Descriptive label for the Field
  • Data Type of the data associated with the Field

Complete PIM javadoc can be read at this link.

Check if the device support's PIM Fields Contact.NAME and Contact.TEL on your device. If not then you need to call PIMList.getSupportedFields() to get the supported field on device and accordingly fetch the name and the telephone number for that device. If I remember correct, the alternative field for name is Contact.FORMATTED_NAME.

0
Chris Mwai On

If you are using the phone number for a text or to call you can do that with only one line of code. Its now evident that phone software issues can affect the way the app accesses the PIM API. Also, if memory in use in the phones contact settings is set to SIM(alone), you can't access contacts in phone memory and vice versa, make sure both are in use. Try this if you still have an issue,

//make a text field in LWUIT that is declared globally

PhnNmbr = new TextField();

//set it to only accept phonenumber

PhnNmbr.setConstraint(TextField.PHONENUMBER);

//tell the user how to access phonebook

PhnNmbr.setHint("Press T9 then 'Search' to search phonebook");

//add a button or command 
//that either sends an sms to
//or calls the number in the text field
//or anything else you want to do with it

When the user presses T9, the TextField is considered an LCDUI text field with parameter PHONENUMBER which allows it to search for contacts in both Sim and phone memory, thats why you'll notice a Search command(usually at the bottom center). Also make sure that the memory in use for the phone is set to both Phone and Sim.