Android 蓝牙扫描代码

/**
 * Created by rbq on 2016/11/1.
 */

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.util.Log;

import java.util.List;

public class BleDevicesScanner implements Runnable {

    private static final String TAG = BleDevicesScanner.class.getSimpleName();

    private static final int DEFAULT_SCAN_PERIOD = 3000;// 2秒钟

    private final BluetoothAdapter adapter;

    private int scanPeriod = DEFAULT_SCAN_PERIOD;

    private BleScanCallback callback;

    private boolean Scaning = false;

    private boolean isstart = false;

    private Handler handler = new Handler();

    private BluetoothLeScanner mScanner;

    private ScanCallback mScanCallback;

    private BluetoothAdapter.LeScanCallback mLeScanCallback;

    public void setCallback(BleScanCallback callback) {
        this.callback = callback;
    }

    private BleScaneStatusChanage scaneStatusChanage;

    public BleScaneStatusChanage getScaneStatusChanage() {
        return scaneStatusChanage;
    }

    public void setScaneStatusChanage(BleScaneStatusChanage scaneStatusChanage) {
        this.scaneStatusChanage = scaneStatusChanage;
    }

    public BleDevicesScanner(Context context) {

        adapter = BleUtils.getBluetoothAdapter(context);

    }

    public synchronized void setScanPeriod(int scanPeriod) {

        this.scanPeriod = scanPeriod < 0 ? DEFAULT_SCAN_PERIOD : scanPeriod;
    }

    public boolean isScanning() {

        return isstart;
    }

    public synchronized void start() {

        if (isstart) {

            return;

        }

        isstart = true;
        handler.post(this);

    }

    public synchronized void stop() {

        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,"蓝牙停止异常");

            }finally {

                Scaning = false;
            }

            Log.i(TAG, "停止扫描");

        }

    }

    @Override
    public void run() {

        if (!adapter.isEnabled()){

            return;
        }

        if (!Scaning) {

            Log.i(TAG, "开始扫描");

            if (isSupportLollipop()) {

                if (mScanner==null) {

                    mScanner = adapter.getBluetoothLeScanner();
                }

                if (mScanner == null) {

                    Scaning = false;

                } else {

                    if (mScanCallback==null){

                        mScanCallback = new ScanCallback() {
                            @Override
                            public void onScanResult(int callbackType, 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);
                                    }
                                }

                            }

                            @Override
                            public void onScanFailed(int errorCode) {
                                super.onScanFailed(errorCode);

                                Log.i(TAG,"扫描失败");

                            }

                            @Override
                            public void onBatchScanResults(List<ScanResult> results) {
                                super.onBatchScanResults(results);

                            }
                        };
                    }

                    mScanner.startScan(mScanCallback);

                    Scaning = true;
                }

            } else {

                if (mLeScanCallback==null){

                    mLeScanCallback = new BluetoothAdapter.LeScanCallback(){

                        @Override
                        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

                            if (callback!=null){

                                callback.onScan(device,rssi,scanRecord);
                            }

                        }
                    };
                }

                Scaning = adapter.startLeScan(mLeScanCallback);
            }

            if (scaneStatusChanage!=null){                scaneStatusChanage.onScaneStart();            }        } else if (Scaning) {            Log.i(TAG, "停止扫描");            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,"蓝牙停止异常");            }            Scaning = false;            if (scaneStatusChanage!=null){                scaneStatusChanage.onScaneStop();            }        }        handler.postDelayed(this, scanPeriod);    }    public boolean isSupportLollipop() {        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;    }}

 

posted @ 2017-03-07 11:11  brave-sailor  阅读(3396)  评论(0编辑  收藏  举报