android - ScanCallback onScanResult 不扫描我的手机
问题描述
我想在两部手机上使用一个应用程序,彼此可以将其扫描为信标。我添加了信标发射器。App nrfConnect 检测到我的手机,但是当我在两台设备上运行我的代码时:
nrF 连接器仅扫描我首先运行该应用程序的第一台设备
当在我的第二部手机上运行该应用程序并调试我的第二部手机以检测我的第一部手机时,找不到我的第一部手机(但可以在 nrfconnector 上找到)
这是我的代码剪断
private String uniqueID;
private BeaconTransmitter mBeaconTransmitter;
Beacon beacon = new Beacon.Builder()
//.setId1("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6")
.setId1(uniqueID)
.setId2("1")
.setId3("2")
.setManufacturer(0x0118)
.setTxPower(-59)
.setDataFields(Arrays.asList(new Long[] {0l}))
.build();
mBeaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
Log.i(TAG, "Advertisement start succeeded.");
}
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
Log.e(TAG, "Advertisement start failed with codeee: " + errorCode);
}
});
@Override
public void onResume() {
mMapView.onResume();
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
if (Build.VERSION.SDK_INT >= 21) {
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
filters = new ArrayList<ScanFilter>();
}
scanLeDevice(true);
}
super.onResume();
}
private void scanLeDevice(final boolean enable) {
if (enable) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT < 21) {
bluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
bluetoothLeScanner.stopScan(mScanCallback);
}
}
}, SCAN_PERIOD);
if (Build.VERSION.SDK_INT < 21) {
bluetoothAdapter.startLeScan(mLeScanCallback);
} else {
bluetoothLeScanner.startScan(filters, settings, mScanCallback);
//bluetoothLeScanner.startScan(mScanCallback);
}
} else {
if (Build.VERSION.SDK_INT < 21) {
bluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
bluetoothLeScanner.stopScan(mScanCallback);
}
}
}
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i("callbackType", String.valueOf(callbackType));
Log.i("name", String.valueOf(result));
String callBackDeviceName = result.getDevice().getName();
String deviceAddress = result.getDevice().getAddress();
//String rssi = result.getRssi;
ScanRecord scanRecord = result.getScanRecord();
SparseArray<byte[]> manufacturerData = scanRecord.getManufacturerSpecificData();
for(int i = 0; i < manufacturerData.size(); i++){
int manufacturerId = manufacturerData.keyAt(i);
if (manufacturerId == 280) { // 0x0118
filterManufacturaId.add(manufacturerId);
}
}
if (callBackDeviceName != null){
if (callBackDeviceName.startsWith("Pi")){
Log.i("result", "Device name: "+callBackDeviceName);
Log.i("result", result.toString());
BluetoothDevice btDevice = result.getDevice();
connectToDevice(btDevice);
}
}
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult sr : results) {
Log.i("ScanResult – Results", sr.toString());
}
}
@Override
public void onScanFailed(int errorCode) {
Log.e("Scan Failed", "Error Code: " + errorCode);
}
};
解决方案
您是否在 Manifest 中设置了位置权限?需要让它为我工作。
推荐阅读
- r - R比例拟合优化
- javascript - NestJS - 如何为响应设置“Access-Control-Allow-Origin”标头
- javascript - Ruby如何将值从单选按钮传递到控制器方法
- html - bootstrap 4 响应式表单,包含带有图像的两列部分
- embedded - STM32陀螺仪角度跟踪
- java - 将单击的项目位置存储在要在其他地方访问的变量中
- django - 使用 Prefetch Django 限制自引用模型的对象数
- javascript - 如何沟通和实时预览跨域应用程序?
- amazon-web-services - AWS 配额的真正最大值是多少?
- django - 如何在多个服务中复制 django 身份验证