Coder Social home page Coder Social logo

bluetoothscanner's Introduction

Android Bluetooth scanner

Make bluetooth more easier. Min SDK 18. Easy to use from LL to M.

compile 'com.rustfisher.hardware:bt-scanner:1.0.1'

Use BtScanner to search bluetooth device.

New a scanner

BtScanner mScanner = new BtScanner(3000);  // Input scan peroid (ms)
// or use defult constructor BtScanner()

Add scan result listener

mScanner.addListener(new BtScanner.Listener() {
            @Override
            public void onDeviceListUpdated(ArrayList<BtDeviceItem> list) {
                // do something..
            }

            @Override
            public void onScanning(boolean scan) {
                // scan status
            }
        });

mScanner.startScan(); // start scan
mScanner.stopScan();  // stop scan

Some configs

mScanner.setScanPeriod(50000);      // Scan time: 50000 ms
mScanner.setNotifyInterval(500);    // Notify scan result every 500 ms
mScanner.setLoadBondDevice(false);  // Output the bonded device ?

Clear listener when your work done. Call this in Activity.onDestroy() or somewhere else.

mScanner.clearListener();

Add ScanFilter and set ScanSettings
Note that SDK version is LL or later

    if (BtScanner.sdkLLOrLater()) {
        mScanner.addScanFilter(new ScanFilter.Builder()
                .setDeviceName("XXX")
                .setDeviceAddress("12:34:56:78:90:EA")
                .build());

        mScanner.setScanSettings(new ScanSettings.Builder()
                .setReportDelay(100)
                .build());
    }

Source code

Use different API by Android SDK version

    if (laterThanLL()) {
        bleScanner.startScan(bleScanCallback);
        Log.d(TAG, "bleScanner startScan");
    } else {
        btAdapter.startLeScan(defBleScanCallback);
        Log.d(TAG, "btAdapter startLeScan");
    }

    private static boolean laterThanLL() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }

A NotifyScanListThread would output the scan result.

About this demo

demo

bluetoothscanner's People

Contributors

rustfisher avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.