android 蓝牙5.0初探之低功耗扫描

来源:互联网 发布:淘宝买家秀大尺度福利 编辑:程序博客网 时间:2024/05/22 09:50

今天闲着没啥事,然后我旁边的IOS同事要学安卓,因为公司是做蓝牙的,我就把我的一个简单的蓝牙demo给了他,然后他在看的时候问我为啥代码中间会有跟横线,于是我也去看了下发现如下:
这里写图片描述
很明显是方法有点老了,于是乎来兴趣了,二话不说直接看谷老大API
https://developer.android.google.cn/about/versions/marshmallow/android-6.0.html#ble-scanning
因为7.0系统没有对蓝牙做啥改变于是我去看了6.0系统的。然后你可以发现很多6.0系统的新特性,找到了一条关键的
这里写图片描述
二话不说咱直接跟进去去看API,6.0突出一个特点就是低功耗,所以咱这个扫描有了个专门的设置类ScanSettings,看下一它的定义:Bluetooth LE scan settings are passed to startScan(ScanCallback) to define the parameters for the scan.很明显意思就是说你可以设置一些扫描参数从而更有效的去扫描出你们自己的产品,这样更节能。
说是ScanSettings其实设置参数啥的还是通过ScanSettings.Builder这个类
看下这个类有啥方法:先new一个这个类
ScanSettings.Builder build=new ScanSettings.Builder();
可以看到
这里写图片描述
他有6个公有的方法具体的介绍还是建议大家自己去看API,
我这边大致说一下这些方法:
1、build ();这个方法返回一个ScanSettings没啥好说的就是构建一个这个类。
2、setCallbackType (int callbackType);这个方法意思就是设置你扫描的回调类型,他可能会抛IllegalArgumentException异常的。
3、setNumOfMatches;这个方法的话上英文吧感觉中文翻译过来怪怪的,“Set the number of matches for Bluetooth LE scan filters hardware match”大概就是说设置匹配数目,这个参数可以设置三个值,具体的是啥看API。这个方法也可能会抛IllegalArgumentException异常的。
4、setReportDelay (long reportDelayMillis);这个方法就是设置汇报的时间戳。也会抛异常。
5、setScanMode (int scanMode);这个方法是设置扫描模式, SCAN_MODE_LOW_POWER, SCAN_MODE_BALANCED or SCAN_MODE_LOW_LATENCY.可传这三个参数,这个方法还是比较重用的以后扫描前可以先设置一下。同样也会抛异常。
6、 setMatchMode (int matchMode);这个方法设置扫描时跟硬件的匹配模式,参数有俩个值可选,同样该方法会抛异常。
这些设置好后,你可检验下是否设置成功,所以当然的Google也写对应的get方法。这个大家自己去API中一一查找。
说了这么多,我们还没开始扫描呢,下面进入咱今天的主角
BluetoothLeScanner,这个就是专门扫描的。
这里写图片描述
马丹编辑器不会用图片都变形了。o(╯□╰)o
这个类主要的方法有如下
这里写图片描述
咳咳必须的好好介绍下了。这里我先试了下2个方法,其他俩个后面的博客再说。先上代码以表敬意`
private void scanLeDevice( Boolean IsDoscan) {
if(IsDoscan){
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.getBluetoothLeScanner().stopScan(callback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.getBluetoothLeScanner().startScan(callback);
}else{
mBluetoothAdapter.getBluetoothLeScanner().stopScan(callback);
mScanning = false;
}

    invalidateOptionsMenu();}//这些是新的扫描可以跟最前面的旧的比较下同样都是一个callback,蓝牙4.0传的是这么个callback,他只要重写一个方法:他这个里面包括三个参数
        private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {        @Override        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {            runOnUiThread(new Runnable() {                @Override                public void run() {                    mLeDeviceListAdapter.addDevice(device);                    mLeDeviceListAdapter.notifyDataSetChanged();                }            });        }    };我们新的是这样的:

ScanCallback callback=new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
bluetoothDevice=result.getDevice();
device_name=bluetoothDevice.getName();
device_address=bluetoothDevice.getAddress();
device_rssi=result.getRssi();
record=result.getScanRecord();
rawData=record.getBytes();
local_name=record.getDeviceName();
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(bluetoothDevice);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});

    }    @Override    public void onBatchScanResults(List<ScanResult> results) {        super.onBatchScanResults(results);    }    @Override    public void onScanFailed(int errorCode) {        super.onScanFailed(errorCode);        //扫描失败返回的参数有4个        //errorCode=1;Fails to start scan as BLE scan with the same settings is already started by the app.        //errorCode=2;Fails to start scan as app cannot be registered.        //errorCode=3;Fails to start scan due an internal error        //errorCode=4;Fails to start power optimized scan as this feature is not supported.    }};

“`
他要重写三个方法,but我们可以抓取更多的数据,我们可以通过onScanFailed(int errorCode)知道扫描失败的原因,通过onBatchScanResults (List results)可以抓取你的扫描记录
最重要的就是onScanResult(int callbackType, ScanResult result) 这个方法,这里介绍下 ScanResult这个类。
照样的先看它的方法:
这里写图片描述
主要用到的就是这getDevice()、getScanRecord()、getRssi()这三个方法,通过这三个get我们可以知道扫描到的设备,信号强度,以及一些扫描记录。
这三点就足够我们写APP了。扫描先介绍到这。很多方法我还没去一一实验,等验证了结果再继续写博客。
首次在这边写博客编辑器不会用,排版也不行。凑合着看吧。还是简书好一点。关于蓝牙欢迎大家与我交流讨论,Android技术交流群 191974931

2 0
原创粉丝点击