历史记录筛选
This commit is contained in:
@@ -52,6 +52,7 @@ import java.util.UUID;
|
|||||||
import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat;
|
import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat;
|
||||||
import no.nordicsemi.android.support.v18.scanner.ScanCallback;
|
import no.nordicsemi.android.support.v18.scanner.ScanCallback;
|
||||||
import no.nordicsemi.android.support.v18.scanner.ScanFilter;
|
import no.nordicsemi.android.support.v18.scanner.ScanFilter;
|
||||||
|
import no.nordicsemi.android.support.v18.scanner.ScanRecord;
|
||||||
import no.nordicsemi.android.support.v18.scanner.ScanResult;
|
import no.nordicsemi.android.support.v18.scanner.ScanResult;
|
||||||
import no.nordicsemi.android.support.v18.scanner.ScanSettings;
|
import no.nordicsemi.android.support.v18.scanner.ScanSettings;
|
||||||
import pub.devrel.easypermissions.EasyPermissions;
|
import pub.devrel.easypermissions.EasyPermissions;
|
||||||
@@ -127,52 +128,58 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|||||||
*/
|
*/
|
||||||
private void startBleScan() {
|
private void startBleScan() {
|
||||||
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
|
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
|
||||||
UUID SERVICE_UUID = UUID.fromString(mUuid);
|
UUID SERVICE_UUID = UUID.fromString(mUuid);//1775244d-6b43-439b-877c-060f2d9bed07
|
||||||
ParcelUuid parcelUuid = new ParcelUuid(SERVICE_UUID);
|
ParcelUuid parcelUuid = new ParcelUuid(SERVICE_UUID);
|
||||||
ScanSettings settings = new ScanSettings.Builder()
|
ScanSettings settings = new ScanSettings.Builder()
|
||||||
.setLegacy(false)
|
|
||||||
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
|
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
|
||||||
.setReportDelay(5000)
|
.setReportDelay(0)
|
||||||
.build();
|
.build();
|
||||||
List<ScanFilter> filters = new ArrayList<>();
|
List<ScanFilter> filters = new ArrayList<>();
|
||||||
filters.add(new ScanFilter.Builder().setServiceUuid(parcelUuid).build());
|
filters.add(new ScanFilter.Builder().setServiceUuid(parcelUuid).build());
|
||||||
|
Log.e("TEST", "开始扫描");
|
||||||
mScanCallback = new ScanCallback() {
|
mScanCallback = new ScanCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onScanResult(int callbackType, @NonNull ScanResult result) {
|
public void onScanResult(int callbackType, @NonNull ScanResult result) {
|
||||||
super.onScanResult(callbackType, result);
|
super.onScanResult(callbackType, result);
|
||||||
if (result.getScanRecord() != null) {
|
String deviceName = result.getScanRecord() != null ?
|
||||||
String deviceName = result.getScanRecord().getDeviceName();
|
result.getScanRecord().getDeviceName() : "无名称";
|
||||||
Log.e("TEST", "onScanResult: " + deviceName);
|
String deviceAddress = result.getDevice().getAddress();
|
||||||
ToastUtil.showShort(mContext, "扫描到设备: " + deviceName);
|
Log.e("TEST", "扫描到: " + deviceName + " (" + deviceAddress + ")");
|
||||||
}else{
|
|
||||||
Log.e("TEST", "onScanResult: " + "设备名称为空");
|
ScanRecord record = result.getScanRecord();
|
||||||
|
|
||||||
|
if (record != null) {
|
||||||
|
|
||||||
|
List<ParcelUuid> uuids =
|
||||||
|
record.getServiceUuids();
|
||||||
|
Log.e("TEST", "uuids=" + uuids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ToastUtil.showShort(mContext, "扫描到: " + deviceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBatchScanResults(@NonNull List<ScanResult> results) {
|
public void onBatchScanResults(@NonNull List<ScanResult> results) {
|
||||||
super.onBatchScanResults(results);
|
super.onBatchScanResults(results);
|
||||||
Log.e("TEST", "onBatchScanResults: " + results.size() + " devices");
|
Log.e("TEST", "批量结果: " + results.size() + " 个设备");
|
||||||
if (results != null && !results.isEmpty()) {
|
|
||||||
ToastUtil.showShort(mContext, "批量扫描到 " + results.size() + " 个设备");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onScanFailed(int errorCode) {
|
public void onScanFailed(int errorCode) {
|
||||||
super.onScanFailed(errorCode);
|
super.onScanFailed(errorCode);
|
||||||
Log.e("TEST", "onScanFailed: " + errorCode);
|
Log.e("TEST", "扫描失败: " + errorCode);
|
||||||
ToastUtil.showShort(mContext, "扫描失败,错误码: " + errorCode);
|
ToastUtil.showShort(mContext, "扫描失败,错误码: " + errorCode);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
scanner.startScan(filters, settings, mScanCallback);
|
scanner.startScan(null, settings, mScanCallback);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化蓝牙适配器
|
* 初始化蓝牙适配器
|
||||||
*/
|
*/
|
||||||
private void initBluetooth() {
|
private void initBluetooth() {
|
||||||
Log.e("TEST", "initBluetooth: 开始初始化");
|
|
||||||
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
|
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
|
||||||
if (bluetoothManager != null) {
|
if (bluetoothManager != null) {
|
||||||
bluetoothAdapter = bluetoothManager.getAdapter();
|
bluetoothAdapter = bluetoothManager.getAdapter();
|
||||||
@@ -189,7 +196,6 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|||||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||||
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
||||||
} else {
|
} else {
|
||||||
Log.e("TEST", "initBluetooth: 蓝牙已开启");
|
|
||||||
bluetoothReady = true;
|
bluetoothReady = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user