Back to project page BluetoothSppPro.
The source code is released under:
Apache License
If you think the Android project BluetoothSppPro listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package mobi.dzs.android.bluetooth; // w w w . ja va2s . c om import java.io.UnsupportedEncodingException; import mobi.dzs.android.util.CHexConver; /** * ??????SPP??? * @version 1.0 2013-03-17 * @author JerryLi (lijian@dzs.mobi) * */ public final class BluetoothSppClient extends BTSerialComm{ /**???:????????16???*/ public final static byte IO_MODE_HEX = 0x01; /**???:???????????*/ public final static byte IO_MODE_STRING = 0x02; /**????????????????*/ private byte mbtTxDMode = IO_MODE_STRING; /**?????????????*/ private byte mbtRxDMode = IO_MODE_STRING; /**?????*/ private byte[] mbtEndFlg = null; /**??:??????? ??????(UTF-8:???????3??/GBK:???????2??)*/ protected String msCharsetName = null; /** * ?????SPP???? * @param String MAC ???MAC???? * @return void * */ public BluetoothSppClient(String MAC){ super(MAC); //????????? } /** * ??????????????? * @param bOutIO_Mode ??io??? IO_MODE_HEX / IO_MODE_STRING * @return void * */ public void setTxdMode(byte bOutputMode){ this.mbtTxDMode = bOutputMode; } /** * ????????????????? * @return byte ??io??? IO_MODE_HEX / IO_MODE_STRING * */ public byte getTxdMode(){ return this.mbtTxDMode; } /** * ?????????????? * @param bOutIO_Mode ??io??? IO_MODE_HEX / IO_MODE_STRING * */ public void setRxdMode(byte bOutputMode){ this.mbtRxDMode = bOutputMode; } /** * ???????????? * * @param byte btData[] ??????????????? * @return int >0 ???????, 0??????????, -2:?????; -3:???? * */ public int Send(String sData){ if (IO_MODE_HEX == this.mbtTxDMode){ //16???????????byte? if (CHexConver.checkHexStr(sData)) return SendData(CHexConver.hexStr2Bytes(sData)); else return 0; //???HEX? }else{ //??????????char?byte??? if (null != this.msCharsetName){ try{ //????????????? return this.SendData(sData.getBytes(this.msCharsetName)); }catch (UnsupportedEncodingException e){ //????????????????? return this.SendData(sData.getBytes()); } } else return this.SendData(sData.getBytes()); } } /** * ???????? * @return String null:???????? / String:???? * */ public String Receive() { byte[] btTmp = this.ReceiveData(); if (null != btTmp){ if (IO_MODE_HEX == this.mbtRxDMode) //16???????????byte? return (CHexConver.byte2HexStr(btTmp, btTmp.length)).concat(" "); else return new String(btTmp); } else return null; } /** * ???????????? * @return void * @see ???ReceiveStopFlg()?? * */ public void setReceiveStopFlg(String sFlg){ this.mbtEndFlg = sFlg.getBytes(); } /** * ?????????(???UTF-8) * @param String sCharset ????? GBK/GB2312 * @return void * @see ?????ReceiveStopFlg()?Send()???? * */ public void setCharset(String sCharset){ this.msCharsetName = sCharset; } /** * ?????????????????????? * ??????????????????????????????????????????? * @return null:????????/String:???????? * */ public String ReceiveStopFlg(){ byte[] btTmp = null; if (null == this.mbtEndFlg) return new String(); //?????? btTmp = this.ReceiveData_StopFlg(this.mbtEndFlg); if (null == btTmp) return null; //????? else{ if (null == this.msCharsetName) return new String(btTmp); else{ try{ //???????????????????? return new String(btTmp, this.msCharsetName); }catch (UnsupportedEncodingException e){ //??????????UTF-8?? return new String(btTmp); } } } } }