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.

4

3 回答 3

2

我在 Android 6.0 API更新中发现了这一点:

改进的低功耗蓝牙扫描

如果您的应用程序执行执行蓝牙低功耗扫描,请使用新的 setCallbackType() 方法指定您希望系统在首次找到或在很长一段时间后看到与设置的 ScanFilter 匹配的广告数据包时通知回调。这种扫描方法比以前平台版本中提供的方法更节能。

于 2016-06-07T18:14:15.857 回答
2

如果您正在扫描心率监测器或接近传感器等 BLE 设备,您的代码应该可以正常工作。问题可能是使用此代码,您的应用程序是 GATT 客户端并且正在搜索 GATT 服务器。

因此,如果您想连接到另一部手机,您可以编写 GATT 服务器应用程序并在另一部手机上运行它(如此处最后一段所示

于 2016-06-14T11:58:54.603 回答
1

谷歌有完整的文档,你可以通过它:

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

于 2016-06-16T05:54:06.317 回答