private void scanLeDevice(final boolean enable) { if (enable) { mHandler.postDelayed( new Runnable() { @Override public void run() { if (Build.VERSION.SDK_INT < 21) { mBluetoothAdapter.stopLeScan(mLeScanCallback); } else { mLEScanner.stopScan(mScanCallback); } } }, SCAN_PERIOD); if (Build.VERSION.SDK_INT < 21) { mBluetoothAdapter.startLeScan(mLeScanCallback); } else { mLEScanner.startScan(filters, settings, mScanCallback); } } else { if (Build.VERSION.SDK_INT < 21) { mBluetoothAdapter.stopLeScan(mLeScanCallback); } else { mLEScanner.stopScan(mScanCallback); } } }
public void startScan() { if (isEnabled()) { if (!isScanning) { System.out.println("starting scan"); scanner = bluetoothAdapter.getBluetoothLeScanner(); ScanFilter.Builder filter = new ScanFilter.Builder(); filter.setServiceUuid(new ParcelUuid(UUID.fromString(B4UUID))); ArrayList<ScanFilter> filters = new ArrayList<>(); filters.add(filter.build()); ScanSettings.Builder settings = new ScanSettings.Builder(); // settings.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY); // settings.setReportDelay(1000L); // scanner.startScan(filters, settings.build(), this); scanner.startScan(this); isScanning = true; } else { System.out.println("stopping scan"); isScanning = false; scanner.flushPendingScanResults(this); scanner.stopScan(this); } } else { System.out.println("BLE not enabled"); } }
public static void stopScan(ScanCallback callback) { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (null == adapter) { Log.e(TAG, "BluetoothAdapter is null"); return; } BluetoothLeScanner scanner = adapter.getBluetoothLeScanner(); if (null == scanner) { Log.e(TAG, "BluetoothLeScanner is null"); return; } scanner.stopScan(callback); }
public void scanLE(@NonNull final ScanFinishListener listener) { BluetoothLeScanner leScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner(); final DashScanCallback callback = new DashScanCallback(); leScanner.startScan(callback); handler = new Handler(); this.listener = listener; finish = new Runnable() { @Override public void run() { BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner().stopScan(callback); devices.putAll( (SimpleArrayMap<? extends BluetoothDevice, ? extends Integer>) callback.devices); listener.onScanFinished(BluetoothScanner.this); } }; handler.postDelayed(finish, 10000); }
@TargetApi(21) private void scanLeDeviceLollipop(final boolean enable) { if (enable) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { lollipopScanner = bluetooth_adapter.getBluetoothLeScanner(); } if (lollipopScanner != null) { Log.d(TAG, "Starting scanner 21"); // Stops scanning after a pre-defined scan period. mHandler.postDelayed( new Runnable() { @Override public void run() { is_scanning = false; if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) { lollipopScanner.stopScan(mScanCallback); } invalidateOptionsMenu(); } }, SCAN_PERIOD); ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(); is_scanning = true; lollipopScanner.startScan(null, settings, mScanCallback); } else { try { scanLeDevice(true); } catch (Exception e) { Log.e(TAG, "Failed to scan for ble device", e); } } } else { is_scanning = false; if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) { lollipopScanner.stopScan(mScanCallback); } } invalidateOptionsMenu(); }
@Override protected void onPause() { super.onPause(); Log.i(TAG, "onPause:+"); if (mMyScanCallBack != null && mBtScanner != null) { Log.i(TAG, "onPause: stopScan"); // BUG: For somereason this didn't stop the scanning!! mBtScanner.stopScan(mMyScanCallBack); } Log.i(TAG, "mSm1: dump"); Log.i(TAG, mSm1.toString()); Log.i(TAG, "mBleScanSm: dump"); Log.i(TAG, mBleScanSm.toString()); Log.i(TAG, "onPause:-"); }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d(TAG, "onActivityResult req:" + requestCode); Log.d(TAG, "onActivityResult res:" + resultCode); if (BT_RESULT == requestCode && resultCode == -1) { List<ScanFilter> scanFilters = new ArrayList<>(); scanFilters.add(new ScanFilter.Builder().setDeviceName("tLTb1507").build()); scanFilters.add(new ScanFilter.Builder().setDeviceName("Kontakt").build()); if (bluetoothLeScanner == null) { bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); } bluetoothLeScanner.startScan(scanFilters, scanSettings, scanCallback); } }
@Override public void onResume() { super.onResume(); if (bluetoothAdapter != null) { if (bluetoothAdapter.isEnabled()) { List<ScanFilter> scanFilters = new ArrayList<>(); scanFilters.add(new ScanFilter.Builder().setDeviceName("tLTb1507").build()); scanFilters.add(new ScanFilter.Builder().setDeviceName("Kontakt").build()); bluetoothLeScanner.startScan(scanFilters, scanSettings, scanCallback); } else { Intent intentBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intentBt, BT_RESULT); } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Intent intent = new Intent(this.getApplicationContext(), WebViewActivity.class); // startActivity(intent); setContentView(R.layout.activity_main); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); // mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); setScanSettings(); mBluetoothLeScanner.startScan(mScanFilter, mScanSettings, mScanCallback); Node[] nodes = {}; adapter = new BeaconListAdapter(this, R.layout.beaconlist); ListView list = (ListView) findViewById(R.id.android_list); list.setAdapter(adapter); list.setOnItemClickListener(adapter); // setAdvertiseData(); // setAdvertiseSettings(); // // if (mBluetoothLeAdvertiser != null) { // mBluetoothLeAdvertiser.startAdvertising(mAdvertiseSettings, mAdvertiseData, new // AdvertiseCallback() { // @Override // public void onStartSuccess(AdvertiseSettings settingsInEffect) { // super.onStartSuccess(settingsInEffect); // Log.d(TAG, "Started Advertising"); // } // // @Override // public void onStartFailure(int errorCode) { // super.onStartFailure(errorCode); // } // }); // } }
private void startScan() { mBtScanner = mBtAdapter.getBluetoothLeScanner(); mMyScanCallBack = new MyScanCallback(); mBtScanner.startScan(mMyScanCallBack); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override protected void doTaskImpl() { final BluetoothLeScanner scanner = mBluetoothManager.getAdapter().getBluetoothLeScanner(); if (scanner != null) { scanner.startScan( new ScanCallback() { @Override public void onScanResult(int callbackType, final ScanResult result) { super.onScanResult(callbackType, result); scanner.stopScan(this); final ScanRecord record = result.getScanRecord(); workInEntries( new WorkInEntriesTask() { @Override public void task( ConcurrentHashMap<String, ConcurrentHashMap<String, String>> entriesRef) { if (record != null) { byte[] bytes = record.getBytes(); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, 5, 24); if (byteBuffer.getInt() == 0x4c000215) { // result bytes to structure. byte[] uuidBytes = new byte[16]; byteBuffer.get(uuidBytes); StringBuilder hexUUIDBuilder = new StringBuilder(); for (int b : uuidBytes) { hexUUIDBuilder.append(Character.forDigit(b >> 4 & 0xf, 16)); hexUUIDBuilder.append(Character.forDigit(b & 0xf, 16)); } final String resultUUID = hexUUIDBuilder.toString().toUpperCase(); final Integer resultMajorId = byteBuffer.getShort() & 0xffff; final Integer resultMinorId = byteBuffer.getShort() & 0xffff; for (ConcurrentHashMap<String, String> entry : entriesRef.values()) { String uuid = entry.get(KEY_UUID); Integer majorId = null; Integer minorId = null; Integer rssiLevel = null; try { majorId = Integer.valueOf(entry.get(KEY_MAJOR_ID)); } catch (Exception e) { // e.printStackTrace(); } try { minorId = Integer.valueOf(entry.get(KEY_MINOR_ID)); } catch (Exception e) { // e.printStackTrace(); } try { rssiLevel = Integer.valueOf(entry.get(KEY_RSSI)); } catch (Exception e) { // e.printStackTrace(); } if (rssiLevel == null) { rssiLevel = Integer.MIN_VALUE; } if (rssiLevel <= result.getRssi()) { int wrongCount = 0; if (uuid != null) { // uuid if (!resultUUID.equals(uuid)) { ++wrongCount; } } if (majorId != null) { // major if (!resultMajorId.equals(majorId)) { ++wrongCount; } } if (minorId != null) { // minor if (!resultMinorId.equals(minorId)) { ++wrongCount; } } if (wrongCount == 0) { if (!mBLEStates.containsKey( entry.get(LocationObserver.KEY_ENTRY_ID))) { boolean enter = Boolean.parseBoolean( entry.get(LocationObserver.KEY_TRANSITION_ENTER)); if (enter) { performIntent( entry.get(LocationObserver.KEY_ENTRY_ID), LocationObserver.Action.Enter); } } mBLEStates.put( entry.get(LocationObserver.KEY_ENTRY_ID), System.currentTimeMillis()); break; } } } } } long currentTimeMillis = System.currentTimeMillis(); for (Iterator<Map.Entry<String, Long>> i = mBLEStates.entrySet().iterator(); i.hasNext(); ) { Map.Entry<String, Long> statesEntry = i.next(); String key = statesEntry.getKey(); Long pastTime = statesEntry.getValue(); if (currentTimeMillis > pastTime + EXIT_SPEND_TIME) { i.remove(); ConcurrentHashMap<String, String> entry = getEntries().get(key); if (entry != null) { boolean exit = Boolean.parseBoolean( entry.get(LocationObserver.KEY_TRANSITION_EXIT)); if (exit) { performIntent(key, LocationObserver.Action.Exit); } } } } mPersistenceManager.saveMemoryToPreferences(); } }); } }); } }
@Override public void onPause() { super.onPause(); if (bluetoothLeScanner != null) bluetoothLeScanner.stopScan(scanCallback); }