Detecting Le Bluetooth Devices on Android

I am starting to develop applications for Android. I tried reading the documentation but getting nothing (functions in the Android tutorial like StartLeScan() are deprecated, etc.)

Is there a simple function that returns a list of bluetooth devices?

something like getDevices() (list of devices)?

thanks

+8
android bluetooth bluetooth-lowenergy
source share
4 answers

It mostly depends on which version of Android you are planning. since api has changed a little in candy (21).

in your activity, get a bluetooth adapter

 BluetoothManager bm = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE) BluetoothAdapter mBluetoothAdapter = bm.getAdapter(); // Ensures Bluetooth is available on the device and it is enabled. If not, // displays a dialog requesting user permission to enable Bluetooth. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } 

then you should check which version of Android you are targeting

 int apiVersion = android.os.Build.VERSION.SDK_INT; if (apiVersion > android.os.Build.VERSION_CODES.KITKAT){ BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner(); // scan for devices scanner.startScan(new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { // get the discovered device as you wish // this will trigger each time a new device is found BluetoothDevice device = result.getDevice(); } }); } else { // targetting kitkat or bellow mBluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { // get the discovered device as you wish } }); // rest of your code that will run **before** callback is triggered since it asynchronous 

Remember to add permissions to the manifest

  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.BLUETOOTH"/> 
+13
source share

if you use an api level less than 21, you will find that StartLeScan is out of date.In the Android candy StartLeScan (), a new scan settings function has appeared. You can use the code below to scan for BLE devices.

 ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder(); scanSettingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER); scanSettings = scanSettingsBuilder.build(); BluetoothScanCallback mScanCallback = new BluetoothScanCallback(); mBluetoothUtils.getBluetoothAdapter().getBluetoothLeScanner() .startScan(scanFilters, scanSettings, mScanCallback); 
+2
source share

I also thought that this problem was in a few hours and I found out the answer by searching the official document.
You need to know 3 classes and the ScanCallback, BluetoothLeScanner, ScanResult method
// my level is so low that I can’t post the link ....
You can see in the Android web browser, the web page will show added to the API level 21 on the right side.

Here is my code modified from this project

 //declare private BluetoothLeScanner mBluetoothLeScanner; mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); //start and stop scan mBluetoothLeScanner.startScan(mScanCallback); mBluetoothLeScanner.stopScan(mScanCallback); //Scan call back function private ScanCallback mScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); mLeDeviceListAdapter.addDevice(result.getDevice()); mLeDeviceListAdapter.notifyDataSetChanged(); } }; 

my grandfather:

 android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "com.polkapolka.bluetooth.le" minSdkVersion 21 targetSdkVersion 22 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } } 

This code works well on my phone, which is API 21. Hope this helps you.

0
source share

I played with BLE scanning for a while until I got the simplest solution from the Scandinavian library.

Add a line to your build.gradle:

 dependencies { .... compile 'no.nordicsemi.android.support.v18:scanner:1.0.0' } 

And use the BluetoothLeScannerCompat:

 import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat; ... BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner(); scaner.startScan(new ScanCallback { ...}); 

And the library does all the work.

0
source share

All Articles