package mydemo; import java.awt.Dimension; import java.awt.Insets; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import phonicapi.PhonicConst; import phonicapi.PhonicHandler; import phonicapi.PhonicNative; public class MainFrame extends JFrame implements PhonicHandler{ private JButton btnHookOff = new JButton("摘机"); private JButton btnHookOn = new JButton("挂机"); private JButton btnCall = new JButton("拨号"); private JButton btnSendMsg = new JButton("发送短信"); private JTextField txtNumb = new JTextField(); private JComboBox cbxLine = new JComboBox(); private JTextField txtShortMsgNumber = new JTextField(); private JTextField txtShortMsg = new JTextField(); private int uBoxHndOne = 0; //线路1操作句柄 private int uBoxHndTwo = 0; //线路2操作句柄 private int uBoxId = 0;// 设备类型 private boolean bHookOff = false; private String strDir = "C:\\"; MainFrame () { try{ File directory = new File(""); strDir = directory.getAbsolutePath(); System.out.println(strDir); }catch(Exception e){} //线路 cbxLine.setBounds(20, 20, 160, 20); cbxLine.addItem("线路1"); cbxLine.addItem("线路2"); cbxLine.setSelectedItem("线路1"); //摘机 btnHookOff.setBounds(20, 60, 60, 20); btnHookOff.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int uBoxHnd = cbxLine.getSelectedIndex()==0?uBoxHndOne:uBoxHndTwo; int nResult = PhonicNative.ubox_soft_hook_off(uBoxHnd); System.out.println(nResult); if(0 == nResult) { bHookOff = true; cbxLine.setEnabled(false); btnHookOff.setEnabled(false); btnHookOn.setEnabled(true); btnCall.setEnabled(true); txtNumb.setEnabled(true); //开始录音 Date nowDate = new Date(); DateFormat dfDate = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); String strPath = strDir+"\\"+dfDate.format(nowDate)+".wav"; System.out.println(strPath); PhonicNative.ubox_record_file(uBoxHnd, strPath, CODER_ALAW); } } }); //挂机 btnHookOn.setEnabled(false); btnHookOn.setBounds(120, 60, 60, 20); btnHookOn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int uBoxHnd = cbxLine.getSelectedIndex()==0?uBoxHndOne:uBoxHndTwo; int nResult = PhonicNative.ubox_soft_hook_on(uBoxHnd); System.out.println(nResult); if(0 == nResult) { bHookOff = false; cbxLine.setEnabled(true); btnHookOff.setEnabled(true); btnHookOn.setEnabled(false); btnCall.setEnabled(true); txtNumb.setEnabled(true); //停止录音 PhonicNative.ubox_stop_record(uBoxHnd); } } }); //拨号 btnCall.setBounds(120, 100, 60, 20); btnCall.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(uBoxId == 8) { if(txtNumb.getText().length() > 0) { cbxLine.setEnabled(false); btnHookOff.setEnabled(false); btnHookOn.setEnabled(true); btnCall.setEnabled(true); txtNumb.setEnabled(true); int uBoxHnd = cbxLine.getSelectedIndex()==0?uBoxHndOne:uBoxHndTwo; int nResult = PhonicNative.ubox_makecall(uBoxHnd, txtNumb.getText()); System.out.println(nResult); } } else { //摘机 if(!bHookOff) { int uBoxHnd = cbxLine.getSelectedIndex()==0?uBoxHndOne:uBoxHndTwo; int nResult = PhonicNative.ubox_soft_hook_off(uBoxHnd); System.out.println(nResult); if(0 == nResult) { bHookOff = true; cbxLine.setEnabled(false); btnHookOff.setEnabled(false); btnHookOn.setEnabled(true); btnCall.setEnabled(true); txtNumb.setEnabled(true); //开始录音 Date nowDate = new Date(); DateFormat dfDate = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); String strPath = strDir+"\\"+dfDate.format(nowDate)+".wav"; System.out.println(strPath); PhonicNative.ubox_record_file(uBoxHnd, strPath, CODER_ALAW); } //等待0.5秒再拨号 try{ Thread.sleep(500); }catch(Exception ee){ ee.printStackTrace(); } } if(txtNumb.getText().length() > 0) { btnCall.setEnabled(false); txtNumb.setEnabled(false); int uBoxHnd = cbxLine.getSelectedIndex()==0?uBoxHndOne:uBoxHndTwo; int nResult = PhonicNative.ubox_send_dtmf(uBoxHnd, txtNumb.getText()); System.out.println(nResult); } } } }); btnCall.setEnabled(false); btnSendMsg.setEnabled(false); //号码输入框 txtNumb.setBounds(20, 100, 80, 20); txtNumb.setText("910000"); txtShortMsgNumber.setBounds(20, 140, 80, 20); //收件号码 txtShortMsgNumber.setText("8615907558923");//发短信,需要在号码前加国际区号 ,中国"86" txtShortMsg.setBounds(120, 140, 200, 20); //短信内容 txtShortMsg.setText("");//发短信,需要在号码前加国际区号 ,中国"86" //发送短信 btnSendMsg.setBounds(340, 140, 120, 20); btnSendMsg.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(uBoxId == 8) { if(txtShortMsgNumber.getText().length() > 0 && txtShortMsg.getText().length() > 0) { int uBoxHnd = cbxLine.getSelectedIndex()==0?uBoxHndOne:uBoxHndTwo; int nResult = PhonicNative.ubox_send_gsm_msg(uBoxHnd, 1, txtShortMsgNumber.getText(), txtShortMsg.getText(),txtShortMsg.getText().length()); System.out.println(nResult); } } } }); //添加控件 add(cbxLine); add(btnHookOff); add(btnHookOn); add(btnCall); add(txtNumb); add(txtShortMsgNumber); add(txtShortMsg); add(btnSendMsg); //初始化 openUBox(); } //打开设备 public boolean openUBox() { PhonicNative.ubox_open_logfile(0); int nOpen = PhonicNative.ubox_open(this, PhonicConst.WORK_MODE_RECORD); System.out.println("ubox_open="+nOpen); if(nOpen == 0) { return true; } return false; } //关闭设备 public boolean closeUBox() { uBoxHndOne = 0; uBoxHndTwo = 0; PhonicNative.ubox_close(); return true; } //设备事件 public void event(int uboxHnd, int eventID, int param1, int param2, int param3, int param4) { switch (eventID) { case UBOX_EVENT_DEVICE_PLUG_IN: { //检测型号正确 //if(1 == PhonicNative.ubox_get_product_ID(uboxHnd)) { if(0 == uBoxHndOne) { uBoxHndOne = uboxHnd; uBoxId = PhonicNative.ubox_get_product_ID(uboxHnd); System.out.println(PhonicNative.ubox_get_device_versionnum(uboxHnd)); System.out.println(PhonicNative.ubox_get_product_name(uboxHnd)); if(uBoxId == 8) //无线设备 { PhonicNative.ubox_set_gsm_work_mode(uboxHnd, 1, 0); //如果不用电话机,关闭电话机模式,否则振铃的时候,戴着耳麦会听到很大的给电话机送来电号码声音,体验效果不好。 } } else if(0 == uBoxHndTwo) { uBoxHndTwo = uboxHnd; } else { uBoxHndOne = uboxHnd; uBoxHndTwo = 0; } } System.out.println("收到设备插入事件:" + uboxHnd); } break; case UBOX_EVENT_DEVICE_PLUG_OUT: { System.out.println("收到设备拔出事件:" + uboxHnd); } break; case UBOX_EVENT_LINE_HOOK_UP: { if(uBoxId == 8) { if(param1 == 1) { System.out.println("收到GSM模块挂机:" + uboxHnd); PhonicNative.ubox_set_talk_link(uboxHnd, EARPHONE_TO_GSM, 0); //被叫能够听不到耳麦说话的声音 } else if(param1 == 2) { System.out.println("收到电话机挂机:" + uboxHnd); } else if(param1 == 3) { System.out.println("收到耳麦挂机:" + uboxHnd); } } else { System.out.println("收到设备挂机事件:" + uboxHnd); } } break; case UBOX_EVENT_LINE_HOOK_OFF: { if(uBoxId == 8) { if(param1 == 0) { System.out.println("收到电话机摘机:" + uboxHnd); } else if(param1 == 2) { System.out.println("收到耳麦摘机:" + uboxHnd); } else if(param1 == 6) { System.out.println("收到GSM模块摘机:" + uboxHnd); PhonicNative.ubox_set_talk_link(uboxHnd, EARPHONE_TO_GSM, 1); //被叫能够听到耳麦说话的声音 } } else { System.out.println("收到设备摘机事件:" + uboxHnd); } } break; case UBOX_EVENT_LINE_RINGING: { System.out.println("收到设备检测线路振铃开始事件:" + uboxHnd); } break; case UBOX_EVENT_DEVICE_PLAY_END: { System.out.println("收到放音结束事件:" + uboxHnd); } break; case UBOX_EVENT_RING_CANCEL: { System.out.println("收到设备检测线路振铃停止事件:" + uboxHnd); } break; case UBOX_EVENT_DEVICE_BUSY_TONE: { System.out.println("收到设备检测线路忙音事件:" + uboxHnd); } break; case UBOX_EVENT_ALARM: { System.out.println("收到设备工作警告事件:" + uboxHnd); } break; case UBOX_EVENT_DEVICE_ERROR: { System.out.println("收到设备错误事件:" + uboxHnd); } break; case UBOX_EVENT_LINE_RING_STOP: { System.out.println("收到设备检测线路振铃闪停事件:" + uboxHnd); } break; case UBOX_EVENT_LINE_HANG: { System.out.println("收到设备悬空事件:" + uboxHnd); } break; case UBOX_EVENT_LINE_VOLTAGE: { System.out.println("收到设备线路当前电压事件:" + uboxHnd); } break; case UBOX_EVENT_STREAM_VOICE: { System.out.println("收到流式录音数据包事件:" + uboxHnd); } break; case UBOX_EVENT_DTMF_DOWN: { //System.out.println("按键事件:" + uboxHnd); } break; case UBOX_EVENT_DTMF_UP: { System.out.println("收到按键释放事件:" + uboxHnd); } break; case UBOX_EVENT_DEVICE_PLAY_ERROR: { System.out.println("收到放音错误事件:" + uboxHnd); } break; case UBOX_EVENT_CALLOUTFINISH: { System.out.println("收到软件拨号完成事件:" + uboxHnd); } break; case UBOX_EVENT_POLARITY: { System.out.println("收到检测极性反转事件:" + uboxHnd); } break; case UBOX_EVENT_SIM_STATE: { if(param2 == 1) { System.out.println("卡状态事件, 当前选择sim卡" + param1); } else if(param2 == 2) { System.out.println("卡状态事件, sim卡" + param1 +" GSM 模块检查Sim卡未插入"); } else if(param2 == 3) { System.out.println("卡状态事件, sim卡" + param1 +" 检测到卡存在,并且是插好的"); } else if(param2 > 3) { System.out.println("卡状态事件, sim卡" + param1 +" 检测到卡有错误,错误值:"+param2); } } break; case UBOX_EVENT_ANSWER: { System.out.println(" 被叫已经应答了"); PhonicNative.ubox_set_talk_link(uboxHnd, EARPHONE_TO_GSM, 1); //被叫能够听到耳麦说话的声音 } break; case UBOX_EVENT_SIGNALE_SIZE: { System.out.println(" 信号大小:"+param1); } break; case UBOX_EVENT_SHORT_MSG_SIZE: { System.out.println(" 被叫已经应答了"); } break; case UBOX_EVENT_SIM_REG: { if(param2 == 0) { System.out.println("sim卡" + param1+1 + " 网络未注册,当前没有搜索到要注册业务的新营运商 "); } else if(param2 == 1) { System.out.println("sim卡" + param1+1 + " 网络已注册"); btnCall.setEnabled(true); //网络已经注册好了,可以拨打电话和发短信 btnSendMsg.setEnabled(true); } else if(param2 == 2) { System.out.println("sim卡" + param1+1 + " 网络未注册,正在搜索要注册业务的新营运商"); } else if(param2 == 3) { System.out.println("sim卡" + param1+1 + " 注册被拒绝"); } else if(param2 == 4) { System.out.println("sim卡" + param1+1 + " 未注册, 未知原因"); } else if(param2 == 5) { System.out.println("sim卡" + param1+1 + " 已注册,漫游"); } } break; case UBOX_EVENT_SHORT_MSG_SEND_REPORT: { if(param1 == 0) { System.out.println("短信发送失败"); } else if(param1 == 1) { System.out.println("短信发送成功"); } else if(param1 == 2) { System.out.println("短信发送超时"); } } break; default: { System.out.println("未知事件[uboxHnd=" + uboxHnd + "][eventID=" + eventID + "][param1=" + param1 + "][param2=" + param2 + "][param3=" + param3 + "][param4=" + param4 + "]"); } break; } } public void callInNumber(int uboxHnd, String number) { String usernum = PhonicNative.ubox_get_usernum(uboxHnd, 32); System.out.println("收到主叫号码[uboxHnd=" + uboxHnd + "][number=" + number + "]"); System.out.println("用户号"+"[usernum=" + usernum + "]"); String get = HttpURLConnectionUtil.doGet("http://xiyangyang.51feijin.com/api/mobile/code?mobile=15179379512"); System.out.println("请求结果" + get); String get2 = HttpURLConnectionUtil.doGet("http://xiyangyang.51feijin.com/company-api/security/popup?code=112233&phone="+number); System.out.println("请求结果" + get2); } public void ShortMsg(int uboxHnd, String PhoneNumber, String stTime, String msg, String strDived) { System.out.println("收到短信[uboxHnd=" + uboxHnd + "][发件人=" + PhoneNumber + "]" + ", 时间:"+stTime +", 内容:" +msg+ " "+strDived); } public void CmdMsg(int uboxHnd, String cmd) { System.out.println("收到消息[uboxHnd=" + uboxHnd + "][内容=" + cmd + "]"); } public static void main(String[] args) { final MainFrame myFrame = new MainFrame(); myFrame.setTitle("电话示例"); myFrame.setSize(800, 600); myFrame.setLayout(null); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = myFrame.getSize(); myFrame.setLocation((screenSize.width-frameSize.width)/2, (screenSize.height-frameSize.height)/2); myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE); myFrame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ myFrame.closeUBox(); } }); myFrame.setVisible(true); System.out.println("退出"); } }