Java Code Examples for android.bluetooth.BluetoothAdapter#LeScanCallback
The following examples show how to use
android.bluetooth.BluetoothAdapter#LeScanCallback .
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
Source File: BluetoothCrashResolver.java From android-sdk with MIT License | 7 votes |
/** * Call this method from your BluetoothAdapter.LeScanCallback method. * Doing so is optional, but if you do, this class will be able to count the number of * disctinct bluetooth devices scanned, and prevent crashes before they happen. * * This works very well if the app containing this class is the only one running bluetooth * LE scans on the device, or it is constantly doing scans (e.g. is in the foreground for * extended periods of time.) * * This will not work well if the application using this class is only scanning periodically * (e.g. when in the background to save battery) and another application is also scanning on * the same device, because this class will only get the counts from this application. * * Future augmentation of this class may improve this by somehow centralizing the list of * unique scanned devices. */ @TargetApi(18) public void notifyScannedDevice(BluetoothDevice device, BluetoothAdapter.LeScanCallback scanner) { int oldSize = 0, newSize; if (isDebugEnabled()) oldSize = distinctBluetoothAddresses.size(); distinctBluetoothAddresses.add(device.getAddress()); if (isDebugEnabled()) { newSize = distinctBluetoothAddresses.size(); if (oldSize != newSize && newSize % 100 == 0) { if (isDebugEnabled()) Log.d(TAG, "Distinct bluetooth devices seen: "+distinctBluetoothAddresses.size()); } } if (distinctBluetoothAddresses.size() > getCrashRiskDeviceCount()) { if (PREEMPTIVE_ACTION_ENABLED && !recoveryInProgress) { Logger.log.verbose("Large number of bluetooth devices detected: "+distinctBluetoothAddresses.size()+" Proactively attempting to clear out address list to prevent a crash"); Logger.log.verbose("Stopping LE Scan"); //noinspection deprecation old API compatability BluetoothAdapter.getDefaultAdapter().stopLeScan(scanner); startRecovery(); processStateChange(); } } }
Example 2
Source File: CycledLeScannerForJellyBeanMr2.java From android-beacon-library with Apache License 2.0 | 6 votes |
private BluetoothAdapter.LeScanCallback getLeScanCallback() { if (leScanCallback == null) { leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) { LogManager.d(TAG, "got record"); mCycledLeScanCallback.onLeScan(device, rssi, scanRecord, System.currentTimeMillis()); if (mBluetoothCrashResolver != null) { mBluetoothCrashResolver.notifyScannedDevice(device, getLeScanCallback()); } } }; } return leScanCallback; }
Example 3
Source File: LegacyScanOperation.java From RxAndroidBle with Apache License 2.0 | 6 votes |
@Override BluetoothAdapter.LeScanCallback createScanCallback(final ObservableEmitter<RxBleInternalScanResultLegacy> emitter) { return new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { if (filterUuids != null && RxBleLog.isAtLeast(LogConstants.DEBUG)) { RxBleLog.d("%s, name=%s, rssi=%d, data=%s", LoggerUtil.commonMacMessage(device.getAddress()), device.getName(), rssi, LoggerUtil.bytesToHex(scanRecord) ); } if (filterUuids == null || uuidUtil.extractUUIDs(scanRecord).containsAll(filterUuids)) { emitter.onNext(new RxBleInternalScanResultLegacy(device, rssi, scanRecord)); } } }; }
Example 4
Source File: ScanOperationApi18.java From RxAndroidBle with Apache License 2.0 | 6 votes |
@Override BluetoothAdapter.LeScanCallback createScanCallback(final ObservableEmitter<RxBleInternalScanResult> emitter) { return new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { if (!scanFilterMatcher.isEmpty() && RxBleLog.isAtLeast(LogConstants.DEBUG) && RxBleLog.getShouldLogScannedPeripherals()) { RxBleLog.d("%s, name=%s, rssi=%d, data=%s", LoggerUtil.commonMacMessage(device.getAddress()), device.getName(), rssi, LoggerUtil.bytesToHex(scanRecord) ); } final RxBleInternalScanResult internalScanResult = scanResultCreator.create(device, rssi, scanRecord); if (scanFilterMatcher.matches(internalScanResult)) { emitter.onNext(internalScanResult); } } }; }
Example 5
Source File: ScanOperationApi18.java From RxAndroidBle with Apache License 2.0 | 5 votes |
@Override boolean startScan(RxBleAdapterWrapper rxBleAdapterWrapper, BluetoothAdapter.LeScanCallback scanCallback) { if (this.scanFilterMatcher.isEmpty()) { RxBleLog.d("No library side filtering —> debug logs of scanned devices disabled"); } return rxBleAdapterWrapper.startLegacyLeScan(scanCallback); }
Example 6
Source File: BLEDevice.java From ibm-wearables-android-sdk with Apache License 2.0 | 5 votes |
public void findDevice(long scanTimeout, final BleFindListener userCallback){ if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Log.i(TAG, "BLE not supported on this device"); return; } if(lastUserCallback != null){ Log.e(TAG, "Already searching for device, you cant call findDevice(...) while the instance is scanning"); return; } lastUserCallback = userCallback; lastScannedDevices.clear(); final BluetoothAdapter.LeScanCallback scanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { ScannedDevice scannedDevice = new ScannedDevice(device, rssi, scanRecord); if(filter.filter(scannedDevice)) { Log.i(TAG, "found " + device.getName() + " - " + device.toString() + " rssi:" + rssi); lastScannedDevices.put(device.getAddress(), scannedDevice); bluetoothDevice = userCallback.onDeviceFound(lastScannedDevices); if(bluetoothDevice != null){ stopLastLeScan(); } } } }; scanLeDevice(scanCallback, scanTimeout); }
Example 7
Source File: BleDevicesScanner.java From Bluefruit_LE_Connect_Android with MIT License | 5 votes |
public BleDevicesScanner(BluetoothAdapter adapter, UUID[] servicesToDiscover, BluetoothAdapter.LeScanCallback callback) { mBluetoothAdapter = adapter; mServicesToDiscover = servicesToDiscover == null ? null : Arrays.asList(servicesToDiscover); mLeScansPoster = new LeScansPoster(callback); mHandler = new Handler(); }
Example 8
Source File: JellyBeanScanner.java From RxCentralBle with Apache License 2.0 | 5 votes |
/** Implementation of Android LeScanCallback. */ private BluetoothAdapter.LeScanCallback getScanCallback() { return (bluetoothDevice, rssi, eirData) -> { if (RxCentralLogger.isDebug()) { RxCentralLogger.debug("onLeScan - BD_ADDR: " + bluetoothDevice.getAddress() + " | RSSI: " + rssi); } ScanData scanData = new JellyBeanScanData(bluetoothDevice, rssi, parsedAdDataFactory.produce(eirData)); scanDataRelay.accept(scanData); }; }
Example 9
Source File: BluetoothCrashResolver.java From android-beacon-library with Apache License 2.0 | 5 votes |
/** * Call this method from your BluetoothAdapter.LeScanCallback method. * Doing so is optional, but if you do, this class will be able to count the number of * distinct Bluetooth devices scanned, and prevent crashes before they happen. * * This works very well if the app containing this class is the only one running bluetooth * LE scans on the device, or it is constantly doing scans (e.g. is in the foreground for * extended periods of time.) * * This will not work well if the application using this class is only scanning periodically * (e.g. when in the background to save battery) and another application is also scanning on * the same device, because this class will only get the counts from this application. * * Future augmentation of this class may improve this by somehow centralizing the list of * unique scanned devices. * * @param device */ @TargetApi(18) public void notifyScannedDevice(BluetoothDevice device, BluetoothAdapter.LeScanCallback scanner) { int oldSize, newSize; oldSize = distinctBluetoothAddresses.size(); synchronized(distinctBluetoothAddresses) { distinctBluetoothAddresses.add(device.getAddress()); } newSize = distinctBluetoothAddresses.size(); if (oldSize != newSize && newSize % 100 == 0) { LogManager.d(TAG, "Distinct Bluetooth devices seen: %s", distinctBluetoothAddresses.size()); } if (distinctBluetoothAddresses.size() > getCrashRiskDeviceCount()) { if (PREEMPTIVE_ACTION_ENABLED && !recoveryInProgress) { LogManager.w(TAG, "Large number of Bluetooth devices detected: %s Proactively " + "attempting to clear out address list to prevent a crash", distinctBluetoothAddresses.size()); LogManager.w(TAG, "Stopping LE Scan"); BluetoothAdapter.getDefaultAdapter().stopLeScan(scanner); startRecovery(); processStateChange(); } } }
Example 10
Source File: LolipopLEScannerTest.java From neatle with MIT License | 5 votes |
@Test @Config(sdk = Build.VERSION_CODES.KITKAT) public void doNotFilterUuidsOnKitkatWithEmptyServiceList() { ByteBuffer mockScanRecordBuffer = ByteBuffer.allocate(100).order(ByteOrder.LITTLE_ENDIAN) .put((byte) 17) .put((byte) 0x07) .putLong(INVALID_UUID.getLeastSignificantBits()) .putLong(INVALID_UUID.getMostSignificantBits()); byte[] mockScanRecord = Arrays.copyOf(mockScanRecordBuffer.array(), mockScanRecordBuffer.position()); Scanner.NewDeviceFoundListener listener = Mockito.mock(Scanner.NewDeviceFoundListener.class); ScannerConfiguration configuration = new ScannerConfiguration(); configuration.setNewDeviceFoundListener(listener); ArgumentCaptor<BluetoothAdapter.LeScanCallback> callbackCaptor = ArgumentCaptor.forClass(BluetoothAdapter.LeScanCallback.class); BluetoothAdapter testAdapter = Mockito.mock(BluetoothAdapter.class); LolipopLEScanner scanner = new LolipopLEScanner(configuration); scanner.onStart(testAdapter, 0); Mockito.verify(testAdapter).startLeScan(callbackCaptor.capture()); callbackCaptor.getValue().onLeScan(Mockito.mock(BluetoothDevice.class), -10, mockScanRecord); Mockito.verify(listener).onNewDeviceFound(Mockito.<ScanEvent>any()); }
Example 11
Source File: P_AndroidBluetoothManager.java From SweetBlue with GNU General Public License v3.0 | 5 votes |
@Override public final boolean startLeScan(BluetoothAdapter.LeScanCallback callback) { if (m_adaptor != null) return m_adaptor.startLeScan(callback); return false; }
Example 12
Source File: UnitTestManagerLayer.java From SweetBlue with GNU General Public License v3.0 | 4 votes |
@Override public void stopLeScan(BluetoothAdapter.LeScanCallback callback) { }
Example 13
Source File: BleDevicesScanner.java From ssj with GNU General Public License v3.0 | 4 votes |
private LeScansPoster(BluetoothAdapter.LeScanCallback leScanCallback) { this.leScanCallback = leScanCallback; }
Example 14
Source File: RxBleAdapterWrapper.java From RxAndroidBle with Apache License 2.0 | 4 votes |
public void stopLegacyLeScan(BluetoothAdapter.LeScanCallback leScanCallback) { bluetoothAdapter.stopLeScan(leScanCallback); }
Example 15
Source File: MainActivity.java From Bluefruit_LE_Connect_Android with MIT License | 4 votes |
private void startScan(final UUID[] servicesToScan) { Log.d(TAG, "startScan"); // Stop current scanning (if needed) stopScanning(); // Configure scanning BluetoothAdapter bluetoothAdapter = BleUtils.getBluetoothAdapter(getApplicationContext()); if (BleUtils.getBleStatus(this) != BleUtils.STATUS_BLE_ENABLED) { Log.w(TAG, "startScan: BluetoothAdapter not initialized or unspecified address."); } else { mScanner = new BleDevicesScanner(bluetoothAdapter, servicesToScan, new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) { //final String deviceName = device.getName(); //Log.d(TAG, "Discovered device: " + (deviceName != null ? deviceName : "<unknown>")); BluetoothDeviceData previouslyScannedDeviceData = null; if (mScannedDevices == null) { mScannedDevices = new ArrayList<>(); // Safeguard } // Check that the device was not previously found for (BluetoothDeviceData deviceData : mScannedDevices) { if (deviceData.device.getAddress().equals(device.getAddress())) { previouslyScannedDeviceData = deviceData; break; } } BluetoothDeviceData deviceData; if (previouslyScannedDeviceData == null) { // Add it to the mScannedDevice list deviceData = new BluetoothDeviceData(); mScannedDevices.add(deviceData); } else { deviceData = previouslyScannedDeviceData; } deviceData.device = device; deviceData.rssi = rssi; deviceData.scanRecord = scanRecord; decodeScanRecords(deviceData); // Update device data long currentMillis = SystemClock.uptimeMillis(); if (previouslyScannedDeviceData == null || currentMillis - mLastUpdateMillis > kMinDelayToUpdateUI) { // Avoid updating when not a new device has been found and the time from the last update is really short to avoid updating UI so fast that it will become unresponsive mLastUpdateMillis = currentMillis; runOnUiThread(new Runnable() { @Override public void run() { updateUI(); } }); } } }); // Start scanning mScanner.start(); } // Update UI updateUI(); }
Example 16
Source File: BleManager.java From BLEChat with GNU General Public License v3.0 | 4 votes |
/***************************************************** * Public methods ******************************************************/ public void setScanCallback(BluetoothAdapter.LeScanCallback cb) { mLeScanCallback = cb; }
Example 17
Source File: ShadowBluetoothLEAdapter.java From blessed-android with MIT License | 4 votes |
@Implementation(minSdk = JELLY_BEAN_MR2) public void stopLeScan(BluetoothAdapter.LeScanCallback callback) { leScanCallbacks.remove(callback); }
Example 18
Source File: ShadowBluetoothLEAdapter.java From blessed-android with MIT License | 4 votes |
@Implementation(minSdk = JELLY_BEAN_MR2) public boolean startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) { // Ignoring the serviceUuids param for now. leScanCallbacks.add(callback); return true; }
Example 19
Source File: P_NativeManagerLayer.java From SweetBlue with GNU General Public License v3.0 | votes |
void stopLeScan(BluetoothAdapter.LeScanCallback callback);
Example 20
Source File: P_NativeManagerLayer.java From AsteroidOSSync with GNU General Public License v3.0 | votes |
void stopLeScan(BluetoothAdapter.LeScanCallback callback);