How to restart BluetoothLeScanner?

2019-04-10 08:59发布

问题:

I'm connecting Android device to iOS using BluetoothLeScanner on Android side. It all works perfectly for the first time.

The thing is that after I connect to one of the discovered iOS devices I stop the scan (stopScan(scanCallback)) to preserve energy.

If later previously connected device for some reason disconnects I again start the scan again... but it seems after the 'restart' scan does not work anymore - it does not discover any more devices... not sure why but I'm forced to restart the app.

Any ideas how to fix this?

I'm also using ScanFilter to filter devices by UUID and use ScanSettings.SCAN_MODE_BALANCED for scan mode.

Here is the code:

private void startScanner() {
    BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(activity.BLUETOOTH_SERVICE);
    BluetoothAdapter adapter = bluetoothManager.getAdapter();
    scanner = adapter.getBluetoothLeScanner();
    ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build();
    scanner.startScan(scanFilters(), settings, scanCallback);
    System.out.println("Scanner started");
}

private List<ScanFilter> scanFilters() {
    ScanFilter filter = new ScanFilter.Builder().setServiceUuid(THE_BT_UUID).build();
    List<ScanFilter> list = new ArrayList<ScanFilter>(1);
    list.add(filter);
    return list;
}



private final ScanCallback scanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        BluetoothDevice device = result.getDevice();
        if (device != null) {
            callback.foundRemoteDevice(BluetoothCentral.this, device);
        }
    }
}


public void connectToDevice(BluetoothDevice device) {
    if (gatt == null) {
        gatt = device.connectGatt(activity, false, gattCallback);
        scanner.stopScan(scanCallback);
    }
}


private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        System.out.println("onConnectionStateChange; status: " + status + ", state: " + newState);

        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                System.out.println("STATE_CONNECTED");
                if (!startedServiceDiscovery) {
                    gatt.discoverServices();
                    startedServiceDiscovery = true;
                }
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                callback.didDisconnectFromDevice(BluetoothCentral.this, gatt.getDevice());
                gatt = null;
                startScanner();
                break;
            default:
                System.out.println("STATE_OTHER");
        }

    }
}

Note that I stop scanner right after connectGatt call and start it again when I get BluetoothProfile.STATE_DISCONNECTED callback.

回答1:

As My comment, helped you to solve the problem, I am adding my comment as an answer here.

Code seems ok to me, did you try adding logs in onScanResult method. to check if device is getting detected but something else is failing. One more thing, I see if I restart scanning it takes time to get the device detected in comparison of first scanning.