Search in sources :

Example 6 with ScanCallback

use of android.bluetooth.le.ScanCallback in project xDrip-plus by jamorham.

the class BluetoothScan method initializeScannerCallback.

@TargetApi(21)
private void initializeScannerCallback() {
    Log.d(TAG, "initializeScannerCallback");
    mScanCallback = new ScanCallback() {

        @Override
        public void onBatchScanResults(final List<ScanResult> results) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    for (ScanResult result : results) {
                        BluetoothDevice device = result.getDevice();
                        if (device.getName() != null && device.getName().length() > 0) {
                            mLeDeviceListAdapter.addDevice(device);
                            try {
                                if (result.getScanRecord() != null)
                                    adverts.put(device.getAddress(), result.getScanRecord().getBytes());
                            } catch (NullPointerException e) {
                            // 
                            }
                        }
                    }
                    mLeDeviceListAdapter.notifyDataSetChanged();
                }
            });
        }

        @Override
        public void onScanResult(int callbackType, final ScanResult result) {
            final BluetoothDevice device = result.getDevice();
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    final String deviceName = device.getName();
                    if (deviceName != null && deviceName.length() > 0) {
                        mLeDeviceListAdapter.addDevice(device);
                        try {
                            if (result.getScanRecord() != null)
                                adverts.put(device.getAddress(), result.getScanRecord().getBytes());
                        } catch (NullPointerException e) {
                        // 
                        }
                        mLeDeviceListAdapter.notifyDataSetChanged();
                    }
                }
            });
        }
    };
}
Also used : ScanResult(android.bluetooth.le.ScanResult) BluetoothDevice(android.bluetooth.BluetoothDevice) ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) ScanCallback(android.bluetooth.le.ScanCallback) TargetApi(android.annotation.TargetApi)

Example 7 with ScanCallback

use of android.bluetooth.le.ScanCallback in project android_packages_apps_Settings by DirtyUnicorns.

the class AnomalyActions method doUnoptimizedBleScan.

private static void doUnoptimizedBleScan(Context ctx, long durationMs) {
    ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
    // perform ble scanning
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Log.e(TAG, "Device does not support Bluetooth or Bluetooth not enabled");
        return;
    }
    BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
    if (bleScanner == null) {
        Log.e(TAG, "Cannot access BLE scanner");
        return;
    }
    ScanCallback scanCallback = new ScanCallback() {

        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.v(TAG, "called onScanResult");
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.v(TAG, "called onScanFailed");
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            Log.v(TAG, "called onBatchScanResults");
        }
    };
    bleScanner.startScan(null, scanSettings, scanCallback);
    try {
        Thread.sleep(durationMs);
    } catch (InterruptedException e) {
        Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
    }
    bleScanner.stopScan(scanCallback);
}
Also used : ScanSettings(android.bluetooth.le.ScanSettings) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanResult(android.bluetooth.le.ScanResult) ScanCallback(android.bluetooth.le.ScanCallback) List(java.util.List) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 8 with ScanCallback

use of android.bluetooth.le.ScanCallback in project platform_packages_apps_Settings by BlissRoms.

the class AnomalyActions method doUnoptimizedBleScan.

private static void doUnoptimizedBleScan(Context ctx, long durationMs) {
    ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
    // perform ble scanning
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Log.e(TAG, "Device does not support Bluetooth or Bluetooth not enabled");
        return;
    }
    BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
    if (bleScanner == null) {
        Log.e(TAG, "Cannot access BLE scanner");
        return;
    }
    ScanCallback scanCallback = new ScanCallback() {

        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.v(TAG, "called onScanResult");
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.v(TAG, "called onScanFailed");
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            Log.v(TAG, "called onBatchScanResults");
        }
    };
    bleScanner.startScan(null, scanSettings, scanCallback);
    try {
        Thread.sleep(durationMs);
    } catch (InterruptedException e) {
        Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
    }
    bleScanner.stopScan(scanCallback);
}
Also used : ScanSettings(android.bluetooth.le.ScanSettings) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanResult(android.bluetooth.le.ScanResult) ScanCallback(android.bluetooth.le.ScanCallback) List(java.util.List) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 9 with ScanCallback

use of android.bluetooth.le.ScanCallback in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class AnomalyActions method doUnoptimizedBleScan.

private static void doUnoptimizedBleScan(Context ctx, long durationMs) {
    ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
    // perform ble scanning
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Log.e(TAG, "Device does not support Bluetooth or Bluetooth not enabled");
        return;
    }
    BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
    if (bleScanner == null) {
        Log.e(TAG, "Cannot access BLE scanner");
        return;
    }
    ScanCallback scanCallback = new ScanCallback() {

        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.v(TAG, "called onScanResult");
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.v(TAG, "called onScanFailed");
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            Log.v(TAG, "called onBatchScanResults");
        }
    };
    bleScanner.startScan(null, scanSettings, scanCallback);
    try {
        Thread.sleep(durationMs);
    } catch (InterruptedException e) {
        Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
    }
    bleScanner.stopScan(scanCallback);
}
Also used : ScanSettings(android.bluetooth.le.ScanSettings) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanResult(android.bluetooth.le.ScanResult) ScanCallback(android.bluetooth.le.ScanCallback) List(java.util.List) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 10 with ScanCallback

use of android.bluetooth.le.ScanCallback in project android-beacon-library by AltBeacon.

the class CycledLeScannerForLollipop method postStopLeScan.

private void postStopLeScan() {
    if (!isBluetoothOn()) {
        LogManager.d(TAG, "Not stopping scan because bluetooth is off");
        return;
    }
    final BluetoothLeScanner scanner = getScanner();
    if (scanner == null) {
        return;
    }
    final ScanCallback scanCallback = getNewLeScanCallback();
    mScanHandler.removeCallbacksAndMessages(null);
    mScanHandler.post(new Runnable() {

        @WorkerThread
        @Override
        public void run() {
            try {
                LogManager.d(TAG, "Stopping LE scan on scan handler");
                scanner.stopScan(scanCallback);
            } catch (IllegalStateException e) {
                LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
            } catch (NullPointerException npe) {
                // Necessary because of https://code.google.com/p/android/issues/detail?id=160503
                LogManager.e(npe, TAG, "Cannot stop scan. Unexpected NPE.");
            } catch (SecurityException e) {
                // Thrown by Samsung Knox devices if bluetooth access denied for an app
                LogManager.e(TAG, "Cannot stop scan.  Security Exception", e);
            }
        }
    });
}
Also used : BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) WorkerThread(androidx.annotation.WorkerThread) ScanCallback(android.bluetooth.le.ScanCallback)

Aggregations

ScanCallback (android.bluetooth.le.ScanCallback)24 BluetoothLeScanner (android.bluetooth.le.BluetoothLeScanner)20 ScanResult (android.bluetooth.le.ScanResult)17 ScanSettings (android.bluetooth.le.ScanSettings)14 RequiresPermission (android.annotation.RequiresPermission)10 BluetoothAdapter (android.bluetooth.BluetoothAdapter)8 List (java.util.List)8 ScanFilter (android.bluetooth.le.ScanFilter)6 ParcelUuid (android.os.ParcelUuid)6 ScanRecord (android.bluetooth.le.ScanRecord)5 RemoteException (android.os.RemoteException)5 ArrayList (java.util.ArrayList)5 UUID (java.util.UUID)5 TargetApi (android.annotation.TargetApi)3 BluetoothDevice (android.bluetooth.BluetoothDevice)2 WorkerThread (androidx.annotation.WorkerThread)2 ActiveBluetoothDevice (com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 RequiresApi (androidx.annotation.RequiresApi)1