如何解决Android Developer Sample BluetoothLeGatt没有扫描结果
我在Galaxy S9上构建并运行了Android Developer示例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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。