Android 开发人员示例 BluetoothLeGatt 没有扫描结果

Android Developer Sample BluetoothLeGatt has no scan results

我在 Galaxy S9 上构建并 运行 Android 开发人员示例 BluetoothLeGatt。它运行,但从不在扫描中显示任何结果。完全没有。 BT 开启,我有设备广播广告数据包。我配对了其中一个,认为它可能需要那个,但仍然没有。

想到什么了吗?我希望基本上能在某种列表中看到办公室周围的大量设备。

谢谢。

确保您在 AndroidManifest.xml 文件中具有以下权限

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

检查 MainActivity.java 的位置权限,如果没有则授予位置权限。

此外,在 AdvertiserService.java 中启动前台通知如下:

String CHANNEL_ID = "44";
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher)
                .setPriority(Notification.PRIORITY_LOW)
                .setOngoing(true)
                .setAutoCancel(false)
                .setContentTitle("Ble DEMO")
                .setContentText("BLE content text")
                .setTicker("TICKER")
                .setChannelId(CHANNEL_ID)
                .setVibrate(new long[]{0L})
                .setContentIntent(pendingIntent);
        Notification notification = builder.build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "NOTIFICATION_BLE_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("NOTIFICATION_BLE_CHANNEL_DESC");
            channel.enableVibration(false);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            notificationManager.createNotificationChannel(channel);
        }
        startForeground(FOREGROUND_NOTIFICATION_ID, notification);