Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

android - Error due to frequent calls to Bluetooth scan

I'm not good at English, so I use a translator to leave questions like this.

Currently, we are looking for a solution for Bluetooth scanning.

Too many Bluetooth scans are returning the following error:

D/Bluetooth LeScanner: Register Scanner () - Status=6 ScannerId=-1ms ScannerID=0

I don't see a solution to this problem, so I leave a question like this.

ScanCallback

public ScanCallback scanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);
        BleDevicesAdd(result);
    }
    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
        for ( ScanResult result : results){
            BleDevicesAdd(result);
        }
    }
    @Override
    public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
        System.out.println("onScanFailed" + errorCode);
    }
};

devicesAdd

public void BleDevicesAdd(ScanResult result){
    BluetoothDevice device = result.getDevice();
    if ( device != null){
        String deviceName = device.getName();
        if ( deviceName != null){
            if ( deviceName.toLowerCase().startsWith("test")){
                boolean bIsExist = false;
                for (BluetoothDevice tmp : devices) {
                    if (tmp.getAddress().equals(device.getAddress())) {
                        bIsExist = true;
                        break;
                    }
                }
                if (!bIsExist) {
                    devices.add(device);
                }
                }
            }
    }

click event

  public void MainClickEvent(){
    binding.btScan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) { 
            mBleScanner.startScan(scanCallback); 
        }
    });

    binding.btStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBleScanner.stopScan(scanCallback);
        }
    });
}

The test situation is repeated as Start -> Stop -> Start -> Stop.

Thank you.

question from:https://stackoverflow.com/questions/65841470/error-due-to-frequent-calls-to-bluetooth-scan

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...