博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 蓝牙扫描代码
阅读量:5121 次
发布时间:2019-06-13

本文共 6221 字,大约阅读时间需要 20 分钟。

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

 

转载于:https://www.cnblogs.com/Free-Thinker/p/6513696.html

你可能感兴趣的文章
ad logon hour
查看>>
获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName...
查看>>
证件照(1寸2寸)拍摄处理知识汇总
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
Python入门-函数
查看>>
[HDU5727]Necklace(二分图最大匹配,枚举)
查看>>
距离公式汇总以及Python实现
查看>>
一道不知道哪里来的容斥题
查看>>
window添加右键菜单
查看>>
Window7上搭建symfony开发环境(PEAR)
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
一些方便系统诊断的bash函数
查看>>
jquery中ajax返回值无法传递到上层函数
查看>>
css3之transform-origin
查看>>
Master选举原理
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
小别离
查看>>