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.
ιε
ε
source share