📄 bluetoothclient.java
字号:
} } /* creating Log methods in Log Form */ synchronized private void log(String msg) { getLogForm().append(msg); getLogForm().append("\n\n"); } private void log(Throwable e) { log(e.getMessage()); } public Enumeration getAllContact(){ try { cl = (ContactList) pim.openPIMList(PIM.CONTACT_LIST,PIM.READ_ONLY); s_contacts = cl.items(); }catch (PIMException e) { log("Error accessing database - aborting action"); }catch (SecurityException e) { log("Okay, this application won't get contacts"); } return s_contacts; } public void getList(){ try { if(cl==null) cl = (ContactList) pim.openPIMList(PIM.CONTACT_LIST,PIM.READ_WRITE); }catch (PIMException e) { log("Error accessing database - aborting action"); }catch (SecurityException e) { log("Okay, this application won't get contacts"); } } /* add contacts to Phone books...*/ public void addToContact(String s){ try { log("In add contact:"+s); String name=""; String phoneNumber[]=null; Enumeration itemsEnum=null; token.setText(s); token.setDelimiters("漏"); Object[] res = token.toArray(); log("length :"+res.length); phoneNumber=new String[res.length-1]; name=(String)res[0]; log("length :"+name); for(int i=1;i<res.length;i++) { phoneNumber[i-1]=(String) res[i]; } Contact searchPattern = null; Contact result = null; log("creating contact."); getList(); result = cl.createContact(); searchPattern = cl.createContact(); searchPattern.addString(Contact.FORMATTED_NAME, Contact.ATTR_NONE,name); for (int i = 0; i < phoneNumber.length; i++) { searchPattern.addString(Contact.TEL,i,phoneNumber[i]); } log("searching contact."); itemsEnum = cl.items(searchPattern); boolean wasFound = false; if (itemsEnum != null && itemsEnum.hasMoreElements()) { wasFound = true; } log("123."); if (!wasFound) { log("contact not found."); /*if (cl.isSupportedArrayElement(Contact.FORMATTED_NAME,0)) { result.addString(Contact.FORMATTED_NAME,0, name); }*/ String[] name1 = new String[cl.stringArraySize(Contact.NAME)]; if (cl.isSupportedArrayElement(Contact.NAME, Contact.NAME_GIVEN)) { name1[Contact.NAME_GIVEN] = name; result.addStringArray(Contact.NAME,PIMItem.ATTR_NONE, name1); } if (cl.isSupportedField(Contact.TEL)) { for (int i = 0; i < phoneNumber.length; i++) { result.addString(Contact.TEL,i,phoneNumber[i]); } } log("creating contact complte."); } result.commit(); log("contact saved..."); }catch(Exception e){ log("data not saved..."+e); } } /* Threads.... */ Runnable contactThread = new Runnable() { public void run() { int j=0; try { Enumeration contacts=getAllContact(); getContactList().deleteAll(); while(contacts.hasMoreElements()) { j++; String name=""; String phoneNumber=""; contact = (Contact) contacts.nextElement(); int telCount = contact.countValues(Contact.TEL); int nameCount = contact.countValues(Contact.NAME); if (telCount > 0 && nameCount > 0 ) { name=contact.getString(Contact.FORMATTED_NAME,0); for (int i = 0; i < telCount; i++) { phoneNumber += contact.getString(Contact.TEL, i)+","; } } else { name=contact.getString(Contact.FORMATTED_NAME,0); for (int i = 0; i < telCount; i++) { phoneNumber += contact.getString(Contact.TEL, i)+","; } } getContactList().append("\n"+j+") "+name+"\n"+phoneNumber+","+telCount+"\n",null); } getContactList().append("Total Conatcts :"+j,null); } catch (Exception e) { log("This application cannot retrieve the contacts"); } try { cl.close(); } catch (PIMException e) {} //disconnect(); destroyAllThread(); switchDisplayable(null,contactList); } }; Runnable contactSynchronize = new Runnable() { public void run() { Enumeration contacts = getAllContact(); int telCount=0; int nameCount=0; int j=0; log("contact fetched.."); if(contacts!=null){ while(contacts.hasMoreElements()) { j++; String name=""; String phoneNumber=""; contact = (Contact) contacts.nextElement(); telCount = contact.countValues(Contact.TEL); nameCount = contact.countValues(Contact.NAME); if (telCount > 0 && nameCount > 0 ) { name=contact.getString(Contact.FORMATTED_NAME,0)+"漏"; for (int i = 0; i < telCount; i++) { phoneNumber += contact.getString(Contact.TEL, i)+"漏"; } } else { name=contact.getString(Contact.FORMATTED_NAME,0)+"漏"; for (int i = 0; i < telCount; i++) { phoneNumber += contact.getString(Contact.TEL, i)+"漏"; } } if(!name.equals("") & !phoneNumber.equals("")){ String temp = name+"漏"+phoneNumber+"庐"; try{ out.write(temp.getBytes()); out.flush(); }catch(Exception e){ log("Error at written time..."+e); } } } log("total data in vector:"+j); /*try{ out.close(); out=null; }catch(Exception e){ log("Error at close time......"+e.toString()); }*/ } getDisplay().vibrate(1000); disconnect(); destroyAllThread(); switchDisplayable(null,menuList); } }; Runnable contactRestore = new Runnable() { char ch; int c; String temp=""; public void run() { try{// out.write(new String("restore").getBytes());// out.flush(); log("waiting for read data...."); while(true){ c = in.read(); if(c==-1){ break; } ch = (char) c; log(""+ch); if(ch!='庐'){ temp =temp+ ch+""; }else{ addToContact(temp); temp=""; } } getDisplay().vibrate(1000); log("add contacts complted...."); switchDisplayable(null,menuList); }catch(Exception e){} /*finally { if (in != null) { try { in.close(); in=null; } catch (IOException e) {} } }*/ disconnect(); destroyAllThread(); } }; /* diconnect connection.. */ public void disconnect(){ try{ if(in!=null && out!=null && stream!=null){ in.close(); out.close(); stream.close(); } }catch(Exception e){ log("Exception at disconnect time :"+e); } finally{ //stream=null; in=null; out=null; log("All connection closed.."); } } public void destroyAllThread(){ if(contactSynchroninzing!=null){ contactSynchroninzing=null; } if(contactRestoring!=null){ contactRestoring=null; } if(browseContact!=null){ browseContact=null; } log("all Thread destroyed..."); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -