-
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);
-
}
-
}
/**
-
扫描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
-
//sacn扫描回调 5.0以上用
-
private ScanCallback mScanCallback = new ScanCallback() {
-
@Override
-
public void onScanResult(
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
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);
-
}
-
}
-
}
-
}
-
};
//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
-
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());
-
}
-
}
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
- /**