2

I've been having some problems getting Bluetooth to scan for devices with my Samsung Galaxy s5.

I'm on Android 6.0 and have set up permissions for my app to scan like so:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            // Android M Permission check
            if (this.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("This app needs location access");
                builder.setMessage("Please grant location access so this app can devices.");
                builder.setPositiveButton(android.R.string.ok, null);
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                    public void onDismiss(DialogInterface dialog) {
                        requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
                    }
                });

                builder.show();
            }

I assume this is working correctly because I got the pop-up asking for permissions which I accepted.

My scan function:

private void scanLeDevice(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    Log.i("start" , "Stopping scan...");
                    mBluetoothLeScanner.stopScan(mScanCallback);
                }
            }, SCAN_PERIOD);

            mScanning = true;
            Log.i("start" , "Starting scan...");
            mBluetoothLeScanner.startScan(mScanCallback);
        } else {
            mScanning = false;
            mBluetoothLeScanner.stopScan(mScanCallback);
        }

    }

Now it stops and starts scanning correctly since Logcat is giving me logs. But it's just not finding any devices which is really weird because I'm sitting next to my laptop and a second phone both with Bluetooth enabled.

Here's my callback by the way, if anyone is interested:

Private ScanCallback mScanCallback = new ScanCallback() {

    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        System.out.println("BLE// onScanResult");
        Log.i("callbackType", String.valueOf(callbackType));
        Log.i("result", result.toString());
        BluetoothDevice btDevice = result.getDevice();
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        System.out.println("BLE// onBatchScanResults");
        for (ScanResult sr : results) {
            Log.i("ScanResult - Results", sr.toString());
        }
    }

    @Override
    public void onScanFailed(int errorCode) {
        System.out.println("BLE// onScanFailed");
        Log.e("Scan Failed", "Error Code: " + errorCode);
    }
};

Now as you can see the scan is not failing since Logcat is not giving me a scan failed log, but apperantly its also not finding any devices...

Logcat:

06-07 17:13:02.622 16802-16802/com.example.joey.findmycar I/start: Starting scan...
06-07 17:13:02.802 16802-16802/com.example.joey.findmycar W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
06-07 17:13:02.882 16802-16802/com.example.joey.findmycar I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@c8fddba time:699666571
06-07 17:13:14.632 16802-16802/com.example.joey.findmycar I/start: Stopping scan...

I've added the correct permissions to my Manifest:

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

I've tried almost every possibility and am starting to think the Bluetooth on my phone might be broken somehow, which is weird because I can manually detect and connect to devices in the Android settings.

joey
  • 241
  • 1
  • 4
  • 16
  • Since you have tried on android 6, have you tried giving location permission as well, as from android 6 onwards location has to be enabled while scaning BLE devices. So you need to ask for runtime permission for location as well – Gautam Jun 07 '16 at 17:43
  • Make sure that location services are enabled also (Settings > Location > On) as if that is turned off, you will not receive any scan results, even if you have location "permission". – Kevin Coppock Jun 07 '16 at 17:57
  • Looks like you have your timeout set to about 10 seconds, is that right? What happens if you just let it go until one of the callbacks is called? – Daniel Nugent Jun 07 '16 at 18:06
  • @kcoppock I tried enabling this but it doesnt seem to matter , still no results when scanning. – joey Jun 07 '16 at 19:08
  • @DanielNugent Ye used to scan for about 10 seconds , tried bumping it up to 100 , didnt make a difference. – joey Jun 07 '16 at 19:09
  • @Gautam So I tried giving runtime permissions for ACCESS_LOCATION_EXTRA_COMMANDS but this doesnt seem to change anything , the app has permission now but still no results coming back. – joey Jun 07 '16 at 19:49
  • @joey just by adding the permission in the manifest it won't work, you have to explicitly ask it via code during runtime. – Gautam Jun 08 '16 at 08:42
  • Is either your laptop or your other mobile phone BLE Servers? I mean, Do they have a GATT ? Possibly this is the problem, if they have no GAP they may not be recognized as BLE devices. – SamuelPS Jun 16 '16 at 07:51

3 Answers3

2

I found this in Android 6.0 APIs updates:

Improved Bluetooth Low Energy Scanning

If your app performs performs Bluetooth Low Energy scans, use the new setCallbackType() method to specify that you want the system to notify callbacks when it first finds, or sees after a long time, an advertisement packet matching the set ScanFilter. This approach to scanning is more power-efficient than what’s provided in the previous platform version.

solosodium
  • 723
  • 5
  • 15
2

Your code should work fine if you are scanning for BLE devices like a heart rate monitor or a proximity sensor. The problem could be that with this code your app is the GATT client and is searching for a GATT server.

So if you want to connect to another phone you could write a GATT server app and run it on the other phone (as indicated in the last paragraph here)

Katharina
  • 1,612
  • 15
  • 27
1

There is complete doc by google u go through it:

https://developer.android.com/guide/topics/connectivity/bluetooth-le.html