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; //from w ww . ja v a 2s.c o m import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.os.AsyncTask; import android.os.Build; import android.os.SystemClock; /** * ?????????? * @version 1.1 2014-05-07 * @author JerryLi (lijian@dzs.mobi) * @see ????????????????SendData()???????????????????<br /> * ??????????????????<br /> * <uses-permission android:name="android.permission.BLUETOOTH"/><br /> * <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/><br /> * Android ????? LEVEL 4?????LEVEL 17???bluetooth 4?ble?? * */ public abstract class BTSerialComm{ /**???:SPP?Service UUID*/ public final static String UUID_SPP = "00001101-0000-1000-8000-00805F9B34FB"; /**?????????50k*/ private static final int iBUF_TOTAL = 1024 * 50; /**?????*/ private final byte[] mbReceiveBufs = new byte[iBUF_TOTAL]; /**????????????????????????*/ private int miBufDataSite = 0; /**?????????*/ private String msMAC; /**????????*/ private boolean mbConectOk = false; /* Get Default Adapter */ private BluetoothAdapter mBT = BluetoothAdapter.getDefaultAdapter(); /**???????????*/ private BluetoothSocket mbsSocket = null; /** ?????? */ private InputStream misIn = null; /** ?????? */ private OutputStream mosOut = null; /**???????*/ private long mlRxd = 0; /**?????????*/ private long mlTxd = 0; /**???????*/ private long mlConnEnableTime = 0; /**??????*/ private long mlConnDisableTime = 0; /**??????????????????????????????????????????????*/ private boolean mbReceiveThread = false; /**???????????????????PV???????????????*/ private final CResourcePV mresReceiveBuf = new CResourcePV(1); /**??????????????????*/ private boolean mbKillReceiveData_StopFlg = false; /**???:??????AsyncTask???(????)*/ private static ExecutorService FULL_TASK_EXECUTOR; /**???:????Adnroid SDK ?????*/ private static final int SDK_VER; static{ FULL_TASK_EXECUTOR = (ExecutorService) Executors.newCachedThreadPool(); SDK_VER = Build.VERSION.SDK_INT; }; /** * ???? * @param String sMAC ???????????MAC?????? * */ public BTSerialComm(String sMAC){ this.msMAC = sMAC; } /** * ????????????? * @return ????? ? * */ public long getConnectHoldTime(){ if (0 == this.mlConnEnableTime) return 0; else if (0 == this.mlConnDisableTime) return (System.currentTimeMillis() - this.mlConnEnableTime) / 1000; else return (this.mlConnDisableTime - this.mlConnEnableTime) / 1000; } /** * ?????????? * @return void * */ public void closeConn(){ if ( this.mbConectOk ){ try{ if (null != this.misIn) this.misIn.close(); if (null != this.mosOut) this.mosOut.close(); if (null != this.mbsSocket) this.mbsSocket.close(); this.mbConectOk = false;//???????? }catch (IOException e){ //??????????????socket?? this.misIn = null; this.mosOut = null; this.mbsSocket = null; this.mbConectOk = false;//???????? }finally{ //????????? this.mlConnDisableTime = System.currentTimeMillis(); } } } /** * ???????????????<br /> * <strong>??</strong>?????????????????????????? * @return boolean false:?????? / true:??????? * */ final public boolean createConn(){ if (! mBT.isEnabled()) return false; //??????????????? if (mbConectOk) this.closeConn(); /*?????????*/ final BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(this.msMAC); final UUID uuidSPP = UUID.fromString(BluetoothSppClient.UUID_SPP); try{ //????????????????SPP?? if (SDK_VER >= 10)//2.3.3???????????????????? this.mbsSocket = device.createInsecureRfcommSocketToServiceRecord(uuidSPP); else//??SPP?? API level 5 this.mbsSocket = device.createRfcommSocketToServiceRecord(uuidSPP); this.mbsSocket.connect(); this.mosOut = this.mbsSocket.getOutputStream();//???????????? this.misIn = this.mbsSocket.getInputStream(); //?????????? this.mbConectOk = true; //??????? this.mlConnEnableTime = System.currentTimeMillis(); //????????? }catch (IOException e){ this.closeConn();//???? return false; }finally{ this.mlConnDisableTime = 0; //?????????? } return true; } /** * ???????????? * @return boolean true:????? / false:???? * */ public boolean isConnect() { return this.mbConectOk; } /** * ??????? * @return long * */ public long getRxd(){ return this.mlRxd; } /** * ????????? * @return long * */ public long getTxd(){ return this.mlTxd; } /** * ???????????? * @return int * */ public int getReceiveBufLen(){ int iBufSize = 0; this.P(this.mresReceiveBuf);//????????????? iBufSize = this.miBufDataSite; this.V(this.mresReceiveBuf);//??????????? return iBufSize; } /** * ????????? * @param byte bD[] ??????????????? * @return int >=0 ???????, -2:?????; -3:???? * */ protected int SendData(byte[] btData){ if (this.mbConectOk){ try{ mosOut.write(btData);//????????? this.mlTxd += btData.length; return btData.length; }catch (IOException e){ //??????????????????socket this.closeConn(); return -3; } } else return -2; } /** * ??????<br /> * <strong>??:</strong>getReceiveBufLen()>0???????????????????????????? * * @return null:????????/byte[]:?????????? * */ final protected synchronized byte[] ReceiveData(){ byte[] btBufs = null; if (mbConectOk){ if (!this.mbReceiveThread){ if(SDK_VER >= 11) //LEVEL 11???????? new ReceiveThread().executeOnExecutor(FULL_TASK_EXECUTOR); else //???????? new ReceiveThread().execute(""); return null; //???????????????? } this.P(this.mresReceiveBuf);//????????????? if (this.miBufDataSite > 0){ btBufs = new byte[this.miBufDataSite]; for(int i=0; i<this.miBufDataSite; i++) btBufs[i] = this.mbReceiveBufs[i]; this.miBufDataSite = 0; } this.V(this.mresReceiveBuf);//??????????? } return btBufs; } /** * ????Byte?????????? * @param src ?????? * @param dest ?????? * @return boolean * */ private static boolean CompByte(byte[] src, byte[] dest){ if (src.length != dest.length) return false; for (int i=0, iLen=src.length; i<iLen; i++) if (src[i] != dest[i]) return false;//?????????????? return true;//??????? } /** * ??????????????????????<br /> * <strong>???:</strong>?????????????????????????????<br /> * <strong>??:</strong>???????????????????????????????????????????<br /> * ????????????????killReceiveData_StopFlg() * @param btStopFlg ????? (??: '\n') * @return null:????????/byte[]:???????? * */ final protected byte[] ReceiveData_StopFlg(byte[] btStopFlg){ int iStopCharLen = btStopFlg.length; //??????? int iReceiveLen = 0; //??????????????????????? byte[] btCmp = new byte[iStopCharLen]; byte[] btBufs = null; //?????? if (mbConectOk){ if (!this.mbReceiveThread){ if(SDK_VER >= 11) //LEVEL 11???????? new ReceiveThread().executeOnExecutor(FULL_TASK_EXECUTOR); else //???????? new ReceiveThread().execute(""); SystemClock.sleep(50);//????????????? } while(true){ this.P(this.mresReceiveBuf);//????????????? iReceiveLen = this.miBufDataSite - iStopCharLen; this.V(this.mresReceiveBuf);//??????????? if (iReceiveLen > 0) break; //???????????????? else SystemClock.sleep(50);//??????????????????? } //????????????????????????? this.mbKillReceiveData_StopFlg = false; //????killReceiveData_StopFlg()?????????? while(this.mbConectOk && !this.mbKillReceiveData_StopFlg){ /*???????????*/ this.P(this.mresReceiveBuf);//????????????? for(int i=0; i<iStopCharLen; i++) btCmp[i] = this.mbReceiveBufs[this.miBufDataSite - iStopCharLen + i]; this.V(this.mresReceiveBuf);//??????????? if (CompByte(btCmp,btStopFlg)){ //?????????? //?????????????????? this.P(this.mresReceiveBuf);//????????????? btBufs = new byte[this.miBufDataSite-iStopCharLen]; //??????? for(int i=0, iLen=this.miBufDataSite-iStopCharLen; i<iLen; i++) btBufs[i] = this.mbReceiveBufs[i]; this.miBufDataSite = 0; this.V(this.mresReceiveBuf);//??????????? break; } else SystemClock.sleep(10);//????????????? } } return btBufs; } /** * ????ReceiveData_StopFlg()???????? * @return void * @see ???ReceiveData_StopFlg()????????????? * */ public void killReceiveData_StopFlg(){ this.mbKillReceiveData_StopFlg = true; } /** * ????P??????????? * @param res CResourcePV ????? * */ private void P(CResourcePV res){ while(!res.seizeRes()) SystemClock.sleep(2);//????????????? } /** * ????V????????? * @param res CResourcePV ????? * */ private void V(CResourcePV res){ res.revert(); //????? } //---------------- /*???????*/ private class ReceiveThread extends AsyncTask<String, String, Integer>{ /**???:???????*/ static private final int BUFF_MAX_CONUT = 1024*5; /**???:????*/ static private final int CONNECT_LOST = 0x01; /**??????????????*/ static private final int THREAD_END = 0x02; /** * ????????????? */ @Override public void onPreExecute(){ mbReceiveThread = true;//?????????? miBufDataSite = 0; //??????0 } @Override protected Integer doInBackground(String... arg0){ int iReadCnt = 0; //?????????? byte[] btButTmp = new byte[BUFF_MAX_CONUT]; //????? /*???????????????????????????*/ while(mbConectOk){ try{ iReadCnt = misIn.read(btButTmp); //????????????????????? }catch (IOException e){ return CONNECT_LOST; } //?????????????? P(mresReceiveBuf);//????????????? mlRxd += iReadCnt; //????????? /*????????????????????????0*/ if ( (miBufDataSite + iReadCnt) > iBUF_TOTAL) miBufDataSite = 0; /*??????????????????*/ for(int i=0; i<iReadCnt; i++) mbReceiveBufs[miBufDataSite + i] = btButTmp[i]; miBufDataSite += iReadCnt; //?????????????? V(mresReceiveBuf);//??????????? } return THREAD_END; } /** * ????????????????? */ @Override public void onPostExecute(Integer result){ mbReceiveThread = false;//?????????? if (CONNECT_LOST == result){ //??????????????? closeConn(); }else{ //????????????? try{ misIn.close(); misIn = null; }catch (IOException e){ misIn = null; } } } } }