手机音频通信的特点通用性强速率低小信号国际标准(CTIA)接口标准国家标准(OMTP)苹果(全系列)、小米(全系列)、魅族(全系列)、索尼(全系列)、LG(全系列)、HTC官方产品、三星(部分)、摩托罗拉部分、诺基亚Lumia和WP7系列、华为(C8812/U9200)、诺基亚、索爱(全系列)、中兴(全系列)、步步高(全系列)、酷派(全系列)、、三星部分旧产品、摩托罗拉XT531、OPPO(全系列)等国产系列手机。1,单向数据传输2,双向数据传输1,手机到设备1,设备到手机1、手机端:a)发送手机端手机端手机发送出来的波形‘1’的频率为1.35KHZ(44100/32)‘0’的频率为2.7KHZ(44100/16)手机端发送流程1publicdoublequeryPresure(){byte[]bt={8,3};sender.send(bt);}}publicclassStreamSenderextendsThread{publicsynchronizedvoidsend(byte[]data){byte[]audiodata=EDPos.encodeData(data);//数据编码,包括加入引导头,帧头,数据长度,结束符等change=true;//还根据数据每一位的‘0/1’编码成相应的波形数据}publicvoidrun(){track=newAudioTrack(STREAM_TYPE,SR,CONF,FORMAT_16,BUFSIZE,MODE);track.play();while(1){if(change)//向设备发送请求track.write(audioData,0,audioData_len);else//发送空闲波形,主要作用于设备休眠功能track.write(idleData,0,idleData_len);}}}publicintinitApp(){setAudioVolume();//设置音量}手机端发送流程2Static//产生正弦波的波形数据{for(intwave_i=0;wave_i128;wave_i++){waveSin[wave_i]=((127*(1-Math.sin((double)2.00*Math.PI*wave_i/128)));}}publicencodeData(byte[]paramArrayOfByte){byte[]dataWave=newbyte[(dataLength+18)*320];}for(i=0;idataLength+Constant.FRAME_HEADER_LEN+5;i++){if(iConstant.FRAME_HEADER_LEN){//加入帧头同步数据sendByte=(byte)0xF0;}elseif(i==Constant.FRAME_HEADER_LEN){//加入帧头数据EB90sendByte=(byte)0xEB;}elseif(i==Constant.FRAME_HEADER_LEN+1){sendByte=(byte)0x91;}elseif(i==Constant.FRAME_HEADER_LEN+2){//加入数据长度dataLength1bytesendByte=(byte)((dataLength)&0xFF);}elseif(idataLength+Constant.FRAME_HEADER_LEN+2){//数据结束符tailsendByte=(byte)0xF0;}else{sendByte=paramArrayOfByte[i-Constant.FRAME_HEADER_LEN-3];}for(j=0;j10;j++){if(j2){sendBit=1;//字节数据头,2bits校验位}else{sendBit=(byte)((sendByte(9-j))&0x01);}if(sendBit==1){//1对应的正弦波数据for(k=0;k128;){//”1”对应的频率1.3khzdataWave[wi++]=waveSin[k];K=k+waveInterval;//waveInterval=4}}else{for(k=0;k128;){//”0”对应的频率2.7khzdataWave[wi++]=waveSin[k];k=k+waveInterval*2;}}}手机端接收数据解码流程this.mHandler=newHandler(){publicvoidhandleMessage(Messagemsg){Meteorological360.this.datHumi.val=(175.72D*val/65536.0D-46.85D);Meteorological360.this.notifyApp(null);}}publicclassStreamReceiverextendsThread{publicvoidrun(){record=newAudioRecord(1,rateHz,16,2,SIZE);//audioSource:MICnum=record.read(BUFDATA,index,SIZE);bt=EDPos.mdecFrame(BUFDATA,num);//对录音数据进行分析解码this.mainHandler.sendMessage(msgs);//给handler发送消息}}byte[]mdecFrame(short[]paramArrayOfShort,intparamInt){booleantmpWave=false;intwaveDataLen=paramInt;recvBytes[0]=0;for(inti=0;iwaveDataLen;i++){nowWaveS=paramArrayOfShort[i];if(preWaveS0&&nowWaveS0&&risingEdgeMask){//检测上升沿tmpWave=true;risingEdgeMask=false;}else{tmpWave=false;}preWaveS=nowWaveS;if(waveCounter==3){//波形周期个数计数器risingEdgeMask=true;}waveCounter++;if(tmpWave){//tmpWavestartswitch(decoderState){caseConstant.STATE_DETECT_WAVE:if(waveCounterConstant.SIGNAL_0_LOW&&waveCounterConstant.SIGNAL_0_HIGH){noweSignal=0;waveDetectCounter=0;}elseif(waveCounterConstant.SIGNAL_0_HIGH&&waveCounterConstant.SIGNAL_1_LOW){//此状态为取反,1,0交叉点if(preSignal==0){noweSignal=1;}elseif(preSignal==1){noweSignal=0;}waveDetectCounter=0;}elseif(waveCounterConstant.SIGNAL_1_LOW&&waveCounterConstant.SIGNAL_1_HIGH){noweSignal=1;waveDetectCounter=0;}else{waveDetectCounter++;if(waveDetectCounter2){//连续3次范围内的周期后,转为接收数据状态decoderState=Constant.STATE_NO_WAVE;//Log.i(DECODE,WAVEdisappear.);waveDetectCounter=0;//状态变量初始化if(headDetected==2){recvBytes[0]=1;//数据接收完毕Log.i(DECODE,WAVEstring=+recvStr);}}}preSignal=noweSignal;bufferByte=(byte)((bufferByte1|noweSignal));if(headDetected==0){if(bufferByte==(byte)0xEB){headDetected=1;counterByte=0;Log.i(DECODE,head0xEBdetected.i=+i);}}elseif(headDetected==1){counterByte++;if(counterByte==10){if(bufferByte==(byte)0x90||bufferByte==(byte)0x91){headDetected=2;Log.i(DECODE,head0x90detected.i=+i);Log.i(DECODE,Lastwavestring=+recvStr);recvStr=;Log.i(TIMECAL,recieve0xEB90:+System.currentTimeMillis());}else{headDetected=0;Log.i(DECODE,head0x90error.i=+i);}counterByte=0;}}elseif(headDetected==2){counterByte++;if(counterByte==10){counterByte=0;//synchronized(recvStr){if(counterString==0){recvLen=bufferByte;Log.i(DECODE,recvLen=+recvLen+,i=+i);/*if(recvLen12||recvLen60){recvBytes[0]=0;returnrecvBytes;}*/}counterString++;recvBytes[counterString]=bufferByte;if(counterString==recvLen+1){decoderState=Constant.STATE_NO_WAVE;waveDetectCounter=0;//状态变量初始化recvBytes[0]=1;//数据接收完毕returnrecvBytes;}//Log.i(DECODE,bufferByte=+bufferByte+,i=+i);//Log.i(DECODE,Receivingstring=+recvStr+,i=+i);}}break;default:break;}waveCounter=0;}//tmpWaveifendif(waveCounter40){waveCounter=0;//Log.i(DECODE,wavelongexceed36.);if(decoderState==Constant.STATE_DETECT_WAVE){decoderState=Constant.STATE_NO_WAVE;waveDetectCounter=0;//状态变量初始化if(headDetected==2){Log.i(DECODE,THISwavestring=+recvStr);recvBytes[0]=1;//数据接收完毕}/*else{Log.i(DECODE,wavenoexist.i=+i);}*/}}}returnrecvBytes;}设备端单片机接收数据解码a)放大电路方式:将手机输出信号经放大电路放大到合适的幅值,然后有锁相环或者结成FSK芯片进行解析。该方式电路较为复杂。b)电压比较器方式:将手机输出的音频信号拉到Vcc/2电平,输入到比较器一端,比较器另一端接Vcc/2电压,这样交流信号即被转化为TTL方波信号,此时再进行解析就变得很简单了。设备端单片机端发送数据实现比较简单:单片机通过定时翻转IO的方式,产生两种不同的频率的方波,频率高为0,频率低的为1。温度T=-46.85+175.72*ST/2^16单