ble兼容Android 4 进行的部分优化,熬夜整理小米Android面试题

本文详细介绍了如何在Android 4.x系统上进行BLE蓝牙低功耗扫描的优化,包括针对不同API级别的适配代码,以及BLE设备扫描回调的实现。同时,展示了如何过滤和管理扫描到的BLE设备,以提高应用性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  2. if (enable){

  3. mHandler.postDelayed(new Runnable() {

  4. @Override

  5. public void run() {

  6. mScaning = false;

  7. mBluetoothAdapter.stopLeScan(mLeScanCallback);

  8. }

  9. },SCAN_SECOND);

  10. mScaning = true;

  11. mBluetoothAdapter.startLeScan(mLeScanCallback);

  12. } else {

  13. mScaning = false;

  14. mBluetoothAdapter.stopLeScan(mLeScanCallback);

  15. }

  16. } else {

  17. scanner = mBluetoothAdapter.getBluetoothLeScanner();

  18. scanner.startScan(mScanCallback);

  19. mHandler.postDelayed(new Runnable() {

  20. @Override

  21. public void run() {

  22. scanner.stopScan(mScanCallback);

  23. }

  24. },SCAN_SECOND);

  25. }

  26. }

/**

  • 扫描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(

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

int callbackType, ScanResult result) {

  1. BluetoothDevice device =result.getDevice();

  2. if (device != null){

  3. //过滤掉其他设备

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

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

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

  7. mBLEDeviceList.add(bleDevice);

  8. showBluetoothLeDevice(bleDevice);

  9. }

  10. }

  11. }

  12. }

  13. @Override

  14. public void onBatchScanResults(List results) {

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

  16. }

  17. @Override

  18. public void onScanFailed(int errorCode) {

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

  20. }

  21. };

  22. //4.3以上

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

  24. @Override

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

  26. if (bluetoothDevice != null){

  27. //过滤掉其他设备

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

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

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

  31. mBLEDeviceList.add(bleDevice);

  32. showBluetoothLeDevice(bleDevice);

  33. }

  34. }

  35. }

  36. }

  37. };

//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

print ?

  1. /**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值