1

I write an Android App that should receive data from a Bluetooth low energy device. As a first step I try to scan for devices. Because the scan did not work I reduced my code to a minimum. When I click the checkPermissionBtn Log shows:

BLE_TESTApp: BLUETOOTH grated.
BLE_TESTApp: BLUETOOTH_ADMIN grated.
BLE_TESTApp: ACCESS_COARSE_LOCATION grated.

So I assume all permissions are o.k.. When I click searchBtn Log shows: BLE_TESTApp: Start scan... after 35 sec. BLE_TESTApp: Stop scan.

But leScanCallback is obviously not called. There is no Device found in the Log.

There is definitely at least one Bluetooth LE device available. I checked it with an Bluetooth LE Terminal App from the Appstore.

Wy does scan not found a device?

Here is my code:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "BLE_TESTApp";

    private Button searchBtn, checkPermissionBtn;

    final int REQ_CODE_BLUETOOTH_PERMISSION = 42;
    final int REQ_CODE_BLUETOOTH_ADMIN_PERMISSION = 43;

    final int REQ_CODE_COARSE_PERMISSION = 44;

    private BluetoothManager bluetoothManager;
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothLeScanner bluetoothLeScannerLeScanner;

    private final static int REQUEST_ENABLE_BT = 1;

    private Handler handler = new Handler();
    private static final long INTERVAL = 35000;

    private ScanCallback leScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.d(TAG, "Device found");
        }
    };

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

        searchBtn = findViewById(R.id.searchBtn);
        checkPermissionBtn = findViewById(R.id.cpBtn);

        checkPermissionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED){
                    Log.d(TAG, "BLUETOOTH grated.");
                }else{
                    Log.d(TAG, "BLUETOOTH NOT grated!");
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BLUETOOTH}, REQ_CODE_BLUETOOTH_PERMISSION);
                }
                if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_GRANTED){
                    Log.d(TAG, "BLUETOOTH_ADMIN grated.");
                }else{
                    Log.d(TAG, "BLUETOOTH_ADMIN NOT grated!");
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BLUETOOTH_ADMIN}, REQ_CODE_BLUETOOTH_ADMIN_PERMISSION);
                }
                if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
                    Log.d(TAG, "ACCESS_COARSE_LOCATION grated.");
                }else{
                    Log.d(TAG, "ACCESS_COARSE_LOCATION NOT grated!");
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQ_CODE_COARSE_PERMISSION);
                }
            }
        });


        bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
        bluetoothLeScannerLeScanner = bluetoothAdapter.getBluetoothLeScanner();

        if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
            Log.d(TAG, "Bluetooth not enabled!");
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }

        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "Start scan...");
                searchBtn.setEnabled(false);

                AsyncTask.execute(new Runnable() {
                    @Override
                    public void run() {
                        bluetoothLeScannerLeScanner.startScan(leScanCallback);
                    }
                });

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.i(TAG, "Stop scan.");
                        searchBtn.setEnabled(true);
                        AsyncTask.execute(new Runnable() {
                            @Override
                            public void run() {
                                bluetoothLeScannerLeScanner.stopScan(leScanCallback);
                            }
                        });
                    }
                }, INTERVAL);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == REQ_CODE_BLUETOOTH_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Log.d(TAG, "REQ_CODE_BLUETOOTH_PERMISSION granted.");
        }
        if(requestCode == REQ_CODE_BLUETOOTH_ADMIN_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Log.d(TAG, "REQ_CODE_BLUETOOTH_ADMIN_PERMISSION granted.");
        }
        if(requestCode == REQ_CODE_COARSE_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Log.d(TAG, "REQ_CODE_COARSE_PERMISSION granted.");
        }
    }
}

And the Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetooth_le_test_03">
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Bluetooth_Le_Test_03">
        <activity android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Rol
  • 11
  • 2

1 Answers1

2

In addition to what you have done, try adding ACCESS_FINE_LOCATION to the list of permissions. Also have a look at the links below for more information on how to do scanning with Android:-

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72