如何在Java应用程序中获取电话号码?

时间:2019-07-16 06:34:15

标签: java serial-port caller-id

我正在开发Java应用程序以获取电话号码。我正在使用USRobotics 5639调制解调器(支持来电显示),并且确实有电话公司提供的来电显示服务。使用超级终端时,我会得到呼叫的号码,时间和日期:
https://imgur.com/wwwRHa7
但是当我尝试在我的应用中执行相同操作时,只会得到以下内容:
ATZ

AT + VCID = 1
OK
这是我目前所拥有的:

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

public class Rxtx
{
    static CommPortIdentifier portId;
    static CommPortIdentifier saveportId;
    static Enumeration  portList;
    static InputStream inputStream;
    static OutputStream outputStream;
    static BufferedInputStream bufferedInputStream;
    static SerialPort serialPort;


    public static void main(String[] args)
    {
        boolean gotPort = false;
        String port;
        portList = CommPortIdentifier.getPortIdentifiers();
        String feedback = null;
        String data = null;

        while(portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if(portId.getName().equals("COM5"))
                {
                    port = portId.getName();
                    gotPort = true;
                }

                if(gotPort == true)
                {
                    try
                    {
                        serialPort = (SerialPort)portId.open("Pruebas", 2000);
                    }
                    catch(PortInUseException ex)
                    {
                        ex.printStackTrace();
                    }
                    try
                    {
                        outputStream = serialPort.getOutputStream();
                    }
                    catch(IOException ex)
                    {
                        ex.printStackTrace();
                    }
                    try
                    {
                        serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                    }
                    catch(UnsupportedCommOperationException ex)
                    {
                        ex.printStackTrace();
                    }
                    try
                    {
                        inputStream = serialPort.getInputStream();
                        bufferedInputStream = new BufferedInputStream(inputStream);
                    }
                    catch(IOException e)
                    {
                        e.printStackTrace();
                    }
                    serialPort.notifyOnDataAvailable(true);
                }
            }
        }

       try
       {
            if(!(outputStream == null))
            {
                String serialMessage = "ATZ\r\n";
                OutputStream outstream = serialPort.getOutputStream();
                outstream.write(serialMessage.getBytes());
                String comando = "AT+VCID=1\r\n";
                OutputStream os = serialPort.getOutputStream();
                os.write(comando.getBytes());

                byte[] readBuffer = new byte[1024];
                boolean read = false;
                while(!read)
                {
                    try
                    {
                        String getInfo = "";
                        Thread.sleep(100);
                        while(bufferedInputStream.available() > 0)
                        {
                            int numBytes = bufferedInputStream.read(readBuffer);
                            getInfo += new String(readBuffer);
                            read = true;
                            data = data + new String(readBuffer, 0, numBytes);
                            data = data.trim();
                        }
                        feedback += getInfo;
                        int length = getInfo.length();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                    finally
                    {
                        System.out.println("data: " + data);
                    }
                }

            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

要获取所有电话号码信息,我想念什么?

1 个答案:

答案 0 :(得分:0)

在您的while(!read)循环中,您可以将其更改为while(true)循环,因为您希望在初始传输后继续接收数据。实际的数据提取将必须由您自己负责,尤其是在NMBR=标签之后。我还清理了一些代码,以便于复制粘贴:


import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

public class Rxtx
{
    static CommPortIdentifier portId;
    static CommPortIdentifier saveportId;
    static Enumeration  portList;
    static InputStream inputStream;
    static OutputStream outputStream;
    static BufferedInputStream bufferedInputStream;
    static SerialPort serialPort;


    public static void main(String[] args)
    {
        String port = "";
        portList = CommPortIdentifier.getPortIdentifiers();
        String feedback = "";
        String data = "";

        while(portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if(portId.getName().equals("COM5"))
                {
                    port = portId.getName();
                    break; // Breaks the loop to next section
                }
            }

        }
        //Cast serialPort
        try
        {
            serialPort = (SerialPort) portId.open("Pruebas", 2000);
        }
        catch(PortInUseException ex)
        {
            ex.printStackTrace();
        }
        //Setup outputStream
        try
        {
            outputStream = serialPort.getOutputStream();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        //set params to serialPort
        try
        {
            serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        }
        catch(UnsupportedCommOperationException ex)
        {
            ex.printStackTrace();
        }
        try
        {
            inputStream = serialPort.getInputStream();
            bufferedInputStream = new BufferedInputStream(inputStream);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        serialPort.notifyOnDataAvailable(true);

       try
       {
            if(!(outputStream == null))
            {
                String serialMessage = "ATZ\r\n"; //Soft reset
                outputStream.write(serialMessage.getBytes());
                String comando = "AT+VCID=1\r\n"; //Caller-Id formatting
                outputStream.write(comando.getBytes());

                byte[] readBuffer = new byte[1024];
                while(true) //Continuously check for data.
                {
                    String getInfo = "";
                    Thread.sleep(100);
                    while(bufferedInputStream.available() > 0)
                    {
                        int numBytes = bufferedInputStream.read(readBuffer);
                        getInfo += new String(readBuffer);
                        data = data + new String(readBuffer, 0, numBytes);
                        data = data.trim();
                    }
                    feedback += getInfo;
                    int length = getInfo.length();
                    System.out.println("data: " + data);
                }

            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}