首页 > 解决方案 > ScanCallback onScanResult 不扫描我的手机

问题描述

我想在两部手机上使用一个应用程序,彼此可以将其扫描为信标。我添加了信标发射器。App nrfConnect 检测到我的手机,但是当我在两台设备上运行我的代码时:

这是我的代码剪断

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);
        }
    };

标签: androidbluetooth-lowenergy

解决方案


您是否在 Manifest 中设置了位置权限?需要让它为我工作。


推荐阅读