Android Bluetooth Scan Code

Source: Internet
Author: User

/*** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.