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

本文共 6160 字,大约阅读时间需要 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; }}

 

转载地址:http://kxktx.baihongyu.com/

你可能感兴趣的文章
三十分钟学完Vue
查看>>
[Contest20180405]抑制「超我」
查看>>
WPF 自定义TreeView控件样式,仿QQ联系人列表
查看>>
mongoDB 文档操作_增
查看>>
Lambda表达式之查询篇
查看>>
JavaScript正则表达式总结
查看>>
node环境下express路由,
查看>>
hdu 1754 I Hate It
查看>>
HelloWorld 模块
查看>>
神经网络-手写字体识别
查看>>
Javascript模拟继承(赠送.net吐槽一段)
查看>>
Problem D
查看>>
数据分析之异常值分析-箱线图
查看>>
会计的思考(31):真正的成本会计在制造部
查看>>
tornado+jsonrpc
查看>>
ecshop如何去掉<a>标签超链接的虚线框
查看>>
C# 无法恢复到按钮设置好的BackColor?只能恢复到Control BackColor(例如灰色)的解决方案:...
查看>>
Java Object类
查看>>
IOS libxml/tree.h file not found 解决方案
查看>>
MySQL数据类型--与MySQL零距离接触2-5字符型
查看>>