如何在我的 Java 应用程序中获取 phone 主叫号码?

How can I get the phone calling number in my Java application?

我正在开发一个 Java 应用程序来获取 phone 主叫号码。我使用的是 USRobotics 5639 调制解调器(支持来电显示),我的 phone 公司确实提供来电显示服务。当我使用超级终端时,我得到电话号码、时间和日期:
https://imgur.com/wwwRHa7
但是当我尝试在我的应用程序中执行相同操作时,我只得到以下信息:
阿兹
好的
AT+VCID=1
好的
这是我目前拥有的:

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();
        }
    }
}

获取来电号码的所有信息,我错过了什么?

在您的while(!read)循环中,您可以将其更改为while(true)循环,因为您希望在初始传输后继续接收数据。实际的数据提取必须由您来完成,特别是在 NMBR= 标记之后。我还清理了您的一些代码,以便于 copy-paste:


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();
        }
    }
}