Java Code Examples for android.bluetooth.le.BluetoothLeScanner#stopScan()

The following examples show how to use android.bluetooth.le.BluetoothLeScanner#stopScan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Stops an ongoing Bluetooth LE device scan.
 *
 * @param callback used to identify which scan to stop must be the same handle used to start the
 * scan
 * @deprecated Use {@link BluetoothLeScanner#stopScan(ScanCallback)} instead.
 */
@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public void stopLeScan(LeScanCallback callback) {
    if (DBG) {
        Log.d(TAG, "stopLeScan()");
    }
    BluetoothLeScanner scanner = getBluetoothLeScanner();
    if (scanner == null) {
        return;
    }
    synchronized (mLeScanClients) {
        ScanCallback scanCallback = mLeScanClients.remove(callback);
        if (scanCallback == null) {
            if (DBG) {
                Log.d(TAG, "scan not started yet");
            }
            return;
        }
        scanner.stopScan(scanCallback);
    }
}
 
Example 2
Source Project: RxCentralBle   File: LollipopScanner.java    License: Apache License 2.0 6 votes vote down vote up
private void stopScan() {
  BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  if (adapter != null && adapter.isEnabled()) {
    BluetoothLeScanner bleScanner = adapter.getBluetoothLeScanner();
    if (bleScanner != null) {
      bleScanner.stopScan(scanCallback);
    } else if (RxCentralLogger.isError()) {
      RxCentralLogger.error("stopScan - BluetoothLeScanner is null!");
    }
  } else if (RxCentralLogger.isError()) {
    if (adapter == null) {
      RxCentralLogger.error("stopScan - Default Bluetooth Adapter is null!");
    } else {
      RxCentralLogger.error("stopScan - Bluetooth Adapter is disabled.");
    }
  }

  scanDataSubject = null;
}
 
Example 3
private void stopScan() {
  BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  if (adapter != null && adapter.isEnabled()) {
    BluetoothLeScanner bleScanner = adapter.getBluetoothLeScanner();
    if (bleScanner != null) {
      bleScanner.stopScan(scanCallback);
    } else if (RxCentralLogger.isError()) {
      RxCentralLogger.error("stopScan - BluetoothLeScanner is null!");
    }
  } else if (RxCentralLogger.isError()) {
    if (adapter == null) {
      RxCentralLogger.error("stopScan - Default Bluetooth Adapter is null!");
    } else {
      RxCentralLogger.error("stopScan - Bluetooth Adapter is disabled.");
    }
  }
}
 
Example 4
@TargetApi(21 /* Build.VERSION_CODES.LOLLIPOP */)
public void stopLeScan(ScanCallback scanCallback) {
    if (!bluetoothAdapter.isEnabled()) {
        // this situation seems to be a problem since API 29
        RxBleLog.v(
                "BluetoothAdapter is disabled, calling BluetoothLeScanner.stopScan(ScanCallback) may cause IllegalStateException"
        );
        // if stopping the scan is not possible due to BluetoothAdapter turned off then it is probably stopped anyway
        return;
    }
    final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
    if (bluetoothLeScanner == null) {
        RxBleLog.w(
                "Cannot call BluetoothLeScanner.stopScan(ScanCallback) on 'null' reference; BluetoothAdapter.isEnabled() == %b",
                bluetoothAdapter.isEnabled()
        );
        // if stopping the scan is not possible due to BluetoothLeScanner not accessible then it is probably stopped anyway
        // this should not happen since the check for BluetoothAdapter.isEnabled() has been added above. This situation was only
        // observed when the adapter was disabled
        return;
    }
    bluetoothLeScanner.stopScan(scanCallback);
}
 
Example 5
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
/* package */ void stopScanInternal(@NonNull final Context context,
									@NonNull final PendingIntent callbackIntent) {
	final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
	final BluetoothLeScanner scanner = adapter.getBluetoothLeScanner();
	if (scanner == null)
		throw new IllegalStateException("BT le scanner not available");

	final PendingIntent pendingIntent = createStoppingPendingIntent(context, callbackIntent);
	scanner.stopScan(pendingIntent);

	synchronized (wrappers) {
		// Do not remove the key, just set the value to null.
		// Based on that we will know that scanning has been stopped.
		// This is used to discard scanning results delivered after the scan was stopped.
		// Unfortunately, the callbackIntent will have to be kept and won't he removed,
		// despite the fact that reports will eventually stop being broadcast.
		wrappers.put(callbackIntent, null);
	}
}
 
Example 6
@Override
@RequiresPermission(allOf = {Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH})
/* package */ void stopScanInternal(@NonNull final ScanCallback callback) {
	ScanCallbackWrapperLollipop wrapper;
	synchronized (wrappers) {
		wrapper = wrappers.remove(callback);
	}
	if (wrapper == null)
		return;

	wrapper.close();

	final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
	if (adapter != null) {
		final BluetoothLeScanner scanner = adapter.getBluetoothLeScanner();
		if (scanner != null)
			scanner.stopScan(wrapper.nativeCallback);
	}
}
 
Example 7
@Override
public void onScanResult(int callbackType, ScanResult result)
{
    //String bleDeviceAddress = getSharedPref().getString("bluetoothDeviceAddress", "unknown");
    //String devName = device.getName() + " [" + device.getAddress() + "]";
    //PodEmuLog.debug("SIBLE: device found - " + devName);

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothLeScanner bluetoothLeScanner = adapter.getBluetoothLeScanner();
    //Do the work below on a worker thread instead!
    bleDevice = result.getDevice();

    bluetoothLeScanner.stopScan(this);
    setState(STATE_CONNECTING);

    bleGATT = bleDevice.connectGatt(baseContext, false, bleGattCallback);
    bleGATT.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);

}
 
Example 8
public static void stopNativeScan(BluetoothAdapter adapter) {
    if (adapter == null)
    {
        Log.e("ScanManager", "Tried to stop the scan, but the Bluetooth Adapter instance was null!");
        return;
    }

    final BluetoothLeScanner scanner = adapter.getBluetoothLeScanner();
    if (scanner != null)
        scanner.stopScan(m_callback);
    else
        Log.w("ScanManager", "Tried to stop the scan, but the BluetoothLeScanner instance was null. This implies the scanning has stopped already.");
}
 
Example 9
@TargetApi(21)
public void close()
{
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothLeScanner bluetoothLeScanner = adapter.getBluetoothLeScanner();
    bluetoothLeScanner.stopScan(new MyScanCallback());

    if(bleGATT!=null) bleGATT.close();
    bleGATT=null;
    bleDevice=null;
    bleScanThread=null;
    mInBuffer.removeAll();
    releaseWriteLock();
}
 
Example 10
Source Project: SweetBlue   File: L_Util.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void stopNativeScan(BluetoothAdapter adapter) {
    if (adapter == null)
    {
        Log.e("ScanManager", "Tried to stop the scan, but the Bluetooth Adapter instance was null!");
        return;
    }

    final BluetoothLeScanner scanner = adapter.getBluetoothLeScanner();
    if (scanner != null)
        scanner.stopScan(m_callback);
    else
        Log.w("ScanManager", "Tried to stop the scan, but the BluetoothLeScanner instance was null. This implies the scanning has stopped already.");
}