Skip to content

Library to scan Bluetooth Low Energy devices in a simple way.

License

Notifications You must be signed in to change notification settings

nutes-uepb/simple-ble-scanner

Repository files navigation

Simple BLE Scanner

license

Library to scan Bluetooth Low Energy devices in a simple way.

*This version works for Android SDK equal to or higher than 21. We will implement for lower versions when necessary.

Features

  • Scanning for BLE devices by name, UUID service or MAC address.

Installation

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
dependencies {
	 implementation 'com.github.nutes-uepb:simple-ble-scanner:v1.0.1'
}

Using

Initialize settings

SimpleBleScanner mScanner = new SimpleBleScanner.Builder()  
        .addFilterServiceUuid(  
            "00001809-0000-1000-8000-00805f9b34fb", // Health Thermometer Service  
            "0000180d-0000-1000-8000-00805f9b34fb" //  Heart Rate Service  
        )  
        .addFilterName("MI Band 2")  
        .addScanPeriod(15000) // 15s in milliseconds
        .build();

Initialize scanning

mScanner.startScan(new SimpleScannerCallback() {  
    @Override  
	public void onScanResult(int callbackType, ScanResult scanResult) {  
        BluetoothDevice device = scanResult.getDevice();  
        Log.d("MainActivity", "Found Device: " + device.toString());  
    }  
  
    @Override  
    public void onBatchScanResults(List<ScanResult> scanResults) {  
        Log.d("MainActivity", "onBatchScanResults(): "+ Arrays.toString(scanResults.toArray()));
    }  
  
    @Override  
    public void onFinish() {  
        Log.d("MainActivity", "onFinish()");
    }  
  
    @Override  
    public void onScanFailed(int errorCode) {  
        Log.d("MainActivity", "onScanFailed() " + errorCode);  
    }  
});

Stop scanning

mScanner.stopScan();