2

是否可以清除发送/接收的数据包数量并重新从 0 开始?

获取发送或接收数据包的代码:

long  no_of_packet_Sent = RadioInfo.getNumberOfPacketsSent();

long  no_of_packet_Received = RadioInfo.getNumberOfPacketsReceived();
4

1 回答 1

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;
于 2012-11-20T09:48:59.780 回答