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

我的梦境 提交于 2021-02-10 14:38:39

问题


I am developing a Java application to get the phone calling number. I'm using a USRobotics 5639 modem (caller ID capable) and I do have the caller id service from my phone company. When I use hyperterminal, I get the number, time and date of the call:
https://imgur.com/wwwRHa7
But when I try to do the same in my app, I only get the following:
ATZ
OK
AT+VCID=1
OK
This is what I have at the moment:

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

What am I missing to get all the information of the calling number?


回答1:


In your while(!read) loop, you could change it to a while(true) loop, because you want to continue to receive data after the initial transmission. The actual data extraction will have to be on your part, specifically after the NMBR= tag. I also cleaned up some of your code, for easy 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();
        }
    }
}


来源:https://stackoverflow.com/questions/57051299/how-can-i-get-the-phone-calling-number-in-my-java-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!