ble兼容Android 4 进行的部分优化

58 阅读5分钟

这时界面上回弹出一个对话框提示我们打开蓝牙,打开成功后会回调Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法,我们可以在里面提示一些信息。比如提示蓝牙已经打开等。

[java] view plain copy

print ?

  1. @Override  

  2. protected void onActivityResult(int requestCode, int resultCode, Intentdata) {  

  3.     super.onActivityResult(requestCode, resultCode, data);  

  4.     if (resultCode != RESULT_OK){  

  5.          return;  

  6.     }  

  7.     if (requestCode == REQUEST_ENABLE_BT){  

  8.          Toast.makeText(this,“蓝牙已开启”,Toast.LENGTH_LONG).show();  

  9.     }  

  10. }  

@Override

protected void onActivityResult(int requestCode, int resultCode, Intentdata) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode != RESULT_OK){

return;

}

if (requestCode == REQUEST_ENABLE_BT){

Toast.makeText(this,"蓝牙已开启",Toast.LENGTH_LONG).show();

}

}

3.扫描周围的BLE蓝牙设备

扫描BLE蓝牙设备,对于4.3以上的系统,直接调用startLeScan(BluetoothAdapter.LeScanCallbackcallback)即可扫描出BLE设备,在callback中会回调。但是对于5.0以上的系统,android添加了新的API,原有的startLeScan(BluetoothAdapter.LeScanCallback callback)已经被废弃,在5.0以上的系统中是使用BluetoothLeScanner的startScan(ScanCallbackcallback),回调也是ScanCallback了。

[java] view plain copy

print ?

  1. /** 

  2.  * 扫描Bluetooth LE 

  3.  * @param enable 

  4.  */  

  5. private void scanBleDevice(boolean enable){  

  6.     //android 5.0 以前  

  7.     if (Build.VERSION.SDK_INT < 21){  

  8.         if (enable){  

  9.             mHandler.postDelayed(new Runnable() {  

  10.                 @Override  

  11.                 public void run() {  

  12.                     mScaning = false;  

  13.                     mBluetoothAdapter.stopLeScan(mLeScanCallback);  

  14.                 }  

  15.             },SCAN_SECOND);  

  16.             mScaning = true;  

  17.             mBluetoothAdapter.startLeScan(mLeScanCallback);  

  18.         } else {  

  19.             mScaning = false;  

  20.             mBluetoothAdapter.stopLeScan(mLeScanCallback);  

  21.         }  

  22.     } else {  

  23.         scanner = mBluetoothAdapter.getBluetoothLeScanner();  

  24.         scanner.startScan(mScanCallback);  

  25.         mHandler.postDelayed(new Runnable() {  

  26.             @Override  

  27.             public void run() {  

  28.                 scanner.stopScan(mScanCallback);  

  29.             }  

  30.         },SCAN_SECOND);  

  31.     }  

  32. }  

/**

  • 扫描Bluetooth LE

  • @param enable

*/

private void scanBleDevice(boolean enable){

//android 5.0 以前

if (Build.VERSION.SDK_INT < 21){

if (enable){

mHandler.postDelayed(new Runnable() {

@Override

public void run() {

mScaning = false;

mBluetoothAdapter.stopLeScan(mLeScanCallback);

}

},SCAN_SECOND);

mScaning = true;

mBluetoothAdapter.startLeScan(mLeScanCallback);

} else {

mScaning = false;

mBluetoothAdapter.stopLeScan(mLeScanCallback);

}

} else {

scanner = mBluetoothAdapter.getBluetoothLeScanner();

scanner.startScan(mScanCallback);

mHandler.postDelayed(new Runnable() {

@Override

public void run() {

scanner.stopScan(mScanCallback);

}

},SCAN_SECOND);

}

}

扫描的回调如下:

[java] view plain copy

print ?

  1. //sacn扫描回调 5.0以上用  

  2. private ScanCallback mScanCallback = new ScanCallback() {  

  3.     @Override  

  4.     public void onScanResult(int callbackType, ScanResult result) {  

  5.          BluetoothDevice device =result.getDevice();  

  6.          if (device != null){  

  7.              //过滤掉其他设备  

  8.              if (device.getName() != null&& device.getName().startsWith(“WINPOS”)){  

  9.                  BLEDevice bleDevice = newBLEDevice(device.getName(),device.getAddress());  

  10.                  if(!mBLEDeviceList.contains(bleDevice)){  

  11.                     mBLEDeviceList.add(bleDevice);  

  12.                     showBluetoothLeDevice(bleDevice);  

  13.                  }  

  14.              }  

  15.          }  

  16.     }  

  17.     @Override  

  18.     public void onBatchScanResults(List results) {  

  19.         Log.d(TAG,”onBatchScanResults”);  

  20.     }  

  21.     @Override  

  22.     public void onScanFailed(int errorCode) {  

  23.         Log.d(TAG,”onScanFailed”);  

  24.     }  

  25. };  

  26. //4.3以上  

  27. private BluetoothAdapter.LeScanCallback mLeScanCallback = newBluetoothAdapter.LeScanCallback() {  

  28.     @Override  

  29.     public void onLeScan(final BluetoothDevice bluetoothDevice, int i,byte[] bytes) {  

  30.          if (bluetoothDevice != null){  

  31.              //过滤掉其他设备  

  32.              if (bluetoothDevice.getName()!= null && bluetoothDevice.getName().startsWith(“WINPOS”)){  

  33.                  BLEDevice bleDevice = newBLEDevice(bluetoothDevice.getName(),bluetoothDevice.getAddress());  

  34.                  if(!mBLEDeviceList.contains(bleDevice)){  

  35.                     mBLEDeviceList.add(bleDevice);  

  36.                     showBluetoothLeDevice(bleDevice);  

  37.                  }  

  38.              }  

  39.          }  

  40.     }  

  41. };  

//sacn扫描回调 5.0以上用

private ScanCallback mScanCallback = new ScanCallback() {

@Override

public void onScanResult(int callbackType, ScanResult result) {

BluetoothDevice device =result.getDevice();

if (device != null){

//过滤掉其他设备

if (device.getName() != null&& device.getName().startsWith("WINPOS")){

BLEDevice bleDevice = newBLEDevice(device.getName(),device.getAddress());

if(!mBLEDeviceList.contains(bleDevice)){

mBLEDeviceList.add(bleDevice);

showBluetoothLeDevice(bleDevice);

}

}

}

}

@Override

public void onBatchScanResults(List results) {

Log.d(TAG,"onBatchScanResults");

}

@Override

public void onScanFailed(int errorCode) {

Log.d(TAG,"onScanFailed");

}

};

//4.3以上

private BluetoothAdapter.LeScanCallback mLeScanCallback = newBluetoothAdapter.LeScanCallback() {

@Override

public void onLeScan(final BluetoothDevice bluetoothDevice, int i,byte[] bytes) {

if (bluetoothDevice != null){

//过滤掉其他设备

if (bluetoothDevice.getName()!= null && bluetoothDevice.getName().startsWith("WINPOS")){

BLEDevice bleDevice = newBLEDevice(bluetoothDevice.getName(),bluetoothDevice.getAddress());

if(!mBLEDeviceList.contains(bleDevice)){

mBLEDeviceList.add(bleDevice);

showBluetoothLeDevice(bleDevice);

}

}

}

}

};

这里我预先定义了BLEDevice类,用来表示BLE设备。定义如下:

[java] view plain copy

print ?

  1. public class BLEDevice {  

  2.    private String name;  

  3.    private String mac;  

  4.    public BLEDevice(String name, String mac) {  

  5.        this.name = name;  

  6.        this.mac = mac;  

  7.    }  

  8.    public String getName() {  

  9.        return name;  

  10.    }  

  11.    public void setName(String name) {  

  12.        this.name = name;  

  13.    }  

  14.    public String getMac() {  

  15.        return mac;  

  16.    }  

  17.    public void setMac(String mac) {  

  18.        this.mac = mac;  

  19.    }  

  20.    @Override  

  21.    public boolean equals(Object o) {  

  22.        return !TextUtils.isEmpty(this.mac) &&!TextUtils.isEmpty(((BLEDevice)o).mac)  

  23.                 &&this.mac.equals(((BLEDevice) o).getMac());  

  24.    }  

  25. }  

public class BLEDevice {

private String name;

private String mac;

public BLEDevice(String name, String mac) {

this.name = name;

this.mac = mac;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getMac() {

return mac;

}

public void setMac(String mac) {

this.mac = mac;

}

@Override

public boolean equals(Object o) {

return !TextUtils.isEmpty(this.mac) &&!TextUtils.isEmpty(((BLEDevice)o).mac)

&&this.mac.equals(((BLEDevice) o).getMac());

}

}

扫描时我将扫描到的设备添加到List mBLEDeviceList中并显示出来。

[java] view plain copy

作者2013年从java开发,转做Android开发,在小厂待过,也去过华为,OPPO等大厂待过,18年四月份进了阿里一直到现在。

参与过不少面试,也当面试官 面试过很多人。深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长,而且极易碰到天花板技术停滞不前!

我整理了一份阿里P7级别的最系统的Android开发主流技术,特别适合有3-5年以上经验的小伙伴深入学习提升。

主要包括阿里,以及字节跳动,腾讯,华为,小米,等一线互联网公司主流架构技术。如果你想深入系统学习Android开发,成为一名合格的高级工程师,可以收藏一下这些Android进阶技术选型

我搜集整理过这几年阿里,以及腾讯,字节跳动,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 Xmind(实际上比预期多花了不少精力),包含知识脉络 + 分支细节。

Java语言与原理; 大厂,小厂。Android面试先看你熟不熟悉Java语言

高级UI与自定义view; 自定义view,Android开发的基本功。

性能调优; 数据结构算法,设计模式。都是这里面的关键基础和重点需要熟练的。

NDK开发; 未来的方向,高薪必会。

前沿技术; 组件化,热升级,热修复,框架设计

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

我在搭建这些技术框架的时候,还整理了系统的高级进阶教程,会比自己碎片化学习效果强太多,GitHub可见;《Android架构视频+学习笔记》

当然,想要深入学习并掌握这些能力,并不简单。关于如何学习,做程序员这一行什么工作强度大家都懂,但是不管工作多忙,每周也要雷打不动的抽出 2 小时用来学习。

不出半年,你就能看出变化!