/*** Created by RBQ on 2016/11/1.*/ImportAndroid.bluetooth.BluetoothAdapter;ImportAndroid.bluetooth.BluetoothDevice;ImportAndroid.bluetooth.le.BluetoothLeScanner;ImportAndroid.bluetooth.le.ScanCallback;ImportAndroid.bluetooth.le.ScanResult;ImportAndroid.content.Context;ImportAndroid.os.Build;ImportAndroid.os.Handler;ImportAndroid.util.Log;Importjava.util.List; Public classBledevicesscannerImplementsRunnable {Private Static FinalString TAG = Bledevicesscanner.class. Getsimplename (); Private Static Final intDefault_scan_period = 3000;//2 seconds Private FinalBluetoothadapter Adapter; Private intScanperiod =Default_scan_period; PrivateBlescancallback callback; Private BooleanScaning =false; Private BooleanIsstart =false; PrivateHandler Handler =NewHandler (); PrivateBluetoothlescanner Mscanner; PrivateScancallback Mscancallback; PrivateBluetoothadapter.lescancallback Mlescancallback; Public voidSetcallback (Blescancallback callback) { This. Callback =callback; } PrivateBlescanestatuschanage Scanestatuschanage; Publicblescanestatuschanage getscanestatuschanage () {returnScanestatuschanage; } Public voidsetscanestatuschanage (Blescanestatuschanage scanestatuschanage) { This. Scanestatuschanage =Scanestatuschanage; } PublicBledevicesscanner (Context context) {Adapter=Bleutils.getbluetoothadapter (context); } Public synchronized voidSetscanperiod (intscanperiod) { This. Scanperiod = Scanperiod < 0?Default_scan_period:scanperiod; } Public Booleanisscanning () {returnIsstart; } Public synchronized voidstart () {if(Isstart) {return; } Isstart=true; Handler.post ( This); } Public synchronized voidStop () {if(!Isstart) { return; } Isstart=false; Handler.removecallbacks ( This); if(scaning) {Try { if(Issupportlollipop ()) {if(Mscanner! =NULL&&mscancallback!=NULL) {Mscanner.stopscan (mscancallback); } } Else { if(Adapter! =NULL&&mlescancallback!=NULL) {Adapter.stoplescan (mlescancallback); } } if(scanestatuschanage!=NULL) {scanestatuschanage.onscanestop (); } } Catch(Exception e) {log.i (TAG,"Bluetooth Stop Exception"); }finally{scaning=false; } log.i (TAG,"Stop Scanning"); }} @Override Public voidrun () {if(!adapter.isenabled ()) { return; } if(!scaning) {log.i (TAG,"Start Scanning"); if(Issupportlollipop ()) {if(mscanner==NULL) {Mscanner=Adapter.getbluetoothlescanner (); } if(Mscanner = =NULL) {scaning=false; } Else { if(mscancallback==NULL) {Mscancallback=NewScancallback () {@Override Public voidOnscanresult (intCallbacktype, scanresult result) { Super. Onscanresult (Callbacktype, result); if(Issupportlollipop ()) {byte[] Scanrecord =NULL; if(Result.getscanrecord ()! =NULL) {Scanrecord=Result.getscanrecord (). GetBytes (); } if(Callback! =NULL) {Callback.onscan (Result.getdevice (), Result.getrssi (), Scanrecord); }}} @Ov Erride Public voidOnscanfailed (intErrorCode) { Super. onscanfailed (ErrorCode); LOG.I (TAG,"Scan Failed"); } @Override Public voidOnbatchscanresults (list<scanresult>results) { Super. Onbatchscanresults (results); } }; } mscanner.startscan (Mscancallback); Scaning=true; } } Else { if(mlescancallback==NULL) {Mlescancallback=NewBluetoothadapter.lescancallback () {@Override Public voidOnlescan (Bluetoothdevice device,intRssibyte[] scanrecord) { if(callback!=NULL) {Callback.onscan (Device,rssi,scanrecord); } } }; } scaning=Adapter.startlescan (Mlescancallback); } if(scanestatuschanage!=NULL) {Scanestatuschanage.onscanestart (); } }Else if(scaning) {LOG.I (TAG, "stop Scanning");synchronized( This) {if(! scaning)return; }Try{if(Issupportlollipop ()) {if(Mscanner! =NULL&&mscancallback!=NULL) {Mscanner.stopscan (mscancallback); } }Else{if(Adapter! =NULL&&mlescancallback!=NULL) {Adapter.stoplescan (mlescancallback); } } }Catch(Exception e) {LOG.I (TAG, "Bluetooth stop Exception"); } scaning =false;if(scanestatuschanage!=NULL) {scanestatuschanage.onscanestop (); }} handler.postdelayed ( This, scanperiod); } Public BooleanIssupportlollipop () {returnBuild.VERSION.SDK_INT >= build.version_codes. LOLLIPOP; }}
Android Bluetooth Scan Code