How can I check bluetooth LE ScanResult with android ble scanning app?

Tags: , ,



Hello I’m working on simple BLE scanning app with java but it doesn’t work.

I referred https://github.com/benlc/ble, android developers and completed the source code.

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private Button btnScanAdvStart;
private ArrayList<ItemDevice> arrayList;
private LeDeviceListAdapter leDeviceListAdapter;
private RecyclerView recyclerViewDevice;
private LinearLayoutManager linearLayoutManager;


private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mBluetoothLeScanner;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnScanAdvStart = findViewById(R.id.btn_start);
    recyclerViewDevice = findViewById(R.id.rv_device);
    linearLayoutManager = new LinearLayoutManager(this);
    recyclerViewDevice.setLayoutManager(linearLayoutManager);

    arrayList = new ArrayList<>();
    leDeviceListAdapter = new LeDeviceListAdapter(arrayList);
    recyclerViewDevice.setAdapter(leDeviceListAdapter);


    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();


    btnScanAdvStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startReceiving();
        }
    });
}

@Override protected void onDestroy() {
    super.onDestroy();
    stopReceiving();
}

private ScanCallback mScanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);

        ItemDevice item = new ItemDevice(
                result.getDevice().getName(),
                result.getDevice().getAddress(),
                result.getRssi(),
                result.getTimestampNanos());

        arrayList.add(item);
        leDeviceListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
    }

    @Override public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
        Toast.makeText(MainActivity.this, "Scan fail", Toast.LENGTH_SHORT).show();
    }
};

private void startReceiving() {
    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    mBluetoothLeScanner.startScan(mScanCallback);
    }

private void stopReceiving() {
    mBluetoothLeScanner.stopScan(mScanCallback);
    }
}

also permission is added in manifest file

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I want to know why it isn’t work and how to make it work. I checked ScanResult with toast message but it does not show any result…

Answer

That’s a very old example. Nowadays Android needs runtime permissions for ACCESS_FINE_LOCATION. See https://developer.android.com/training/permissions/requesting for a tutorial.



Source: stackoverflow