2012-11-12 76 views
2

是否可以清除發送/接收的數據包數量並從0開始?清除發送/接收的數據包

代碼即可獲得發送或接收數據包:

long no_of_packet_Sent = RadioInfo.getNumberOfPacketsSent(); 

long no_of_packet_Received = RadioInfo.getNumberOfPacketsReceived(); 

回答

1

我從來沒有發現這個問題的答案,但另一種選擇是從寫在文本文件中的數據,然後一個文本文件,減去數據「獲取數據包數量「。

private static String fileFormatString(String filename) { 
    return filename.replace(" ".charAt(0),"_".charAt(0)); 
} 
public static String readTextFile(String fName) { 
    fName = fileFormatString(fName); 
    String result = null; 
    FileConnection fconn = null; 
    DataInputStream is = null; 
    try { 
     fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE); 
     is = fconn.openDataInputStream(); 
     byte[] data = IOUtilities.streamToBytes(is); 
     result = new String(data); 
    } catch (IOException e) { 
     System.out.println("Error on read: "+fName+" - " + e.getMessage()); 
    } finally { 
     try { 
      if (null != is) is.close(); 
      if (null != fconn) fconn.close(); 
     } catch (IOException e) { 
      System.out.println("Error on read IO: "+fName+" - " + e.getMessage()); 
     } 
    } 
    return result; 
} 
public static void writeTextFile(String fName, String text) { 
    fName = fileFormatString(fName); 
    DataOutputStream os = null; 
    FileConnection fconn = null; 
    try { 
     fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE); 
     if (fconn.exists()); 
     if (!fconn.exists()) fconn.create(); 
     os = fconn.openDataOutputStream(); 
     os.write(text.getBytes()); 
    } catch (IOException e) { 
     System.out.println("Error on write: "+fName+" - " + e.getMessage()); 
    } finally { 
     try { 
      if (null != os) os.close(); 
      if (null != fconn) fconn.close(); 
     } catch (IOException e) { 
      System.out.println("Error on write IO: "+fName+" - " + e.getMessage()); 
     } 
    } 
} 
long no_of_packet = RadioInfo.getNumberOfPacketsSent()+RadioInfo.getNumberOfPacketsReceived(); 
DTHelper.writeTextFile(text_file_name,""+no_of_packet); 

String readnumberofkbytes=readTextFile(text_file_name); 
long Longreadnumberofbytes = Long.parseLong(readnumberofbytes); 

long CurrentNumberofDataUsed= no_of_packet -Longreadnumberofbytes;