MainFrame.java 13.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
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("退出");

	}
}