当前位置:   article > 正文

Android开发中遇到mBluetoothAdapter.startDiscovery()搜索不到任何蓝牙设备

bluetoothadapter.startdiscovery()

最近在更新开发公司的APP应用程序,版本已经都开发完成了,准备做发布的时候。突然我们的一个程序员反馈,在他的手机上测试,APP程序无法搜索到任何的蓝牙设备。于是我就懵逼了,因为APP程序已经在Android 6.0,9.0的几台真机上都测试通过了呀,都能够正常搜索到需要连接的蓝牙设备,而且数据通信和控制功能都非常正常。对方的手机系统为HomnnoyOS 2.0.1,难道华为的鸿蒙系统存在不兼容的情况。
拿对方的手机,连接上开发者模式,最终发现系统跑到mBluetoothAdapter.startDiscovery()之后,就没有接收到系统的蓝牙广播消息,所有就无法更新listview来显示搜索到的蓝牙设备。
经过多方查看,最终得到的结论是在Android 10以上的版本,蓝牙设备搜索需要开启定位功能。结果在原来的手机上,开启了定位功能,果然可以搜索到久违的蓝牙设备了,并且可以正常连接和完成系统功能。
百度上没有搜索到很有用的中文相关内容,最后Google在Stackoverflow上面找到一篇比较接地气的内容,解决了我所遇到的问题,非常感谢这位楼主和提供正确信息的高手。
https://stackoverflow.com/questions/61792203/bluetooth-startdiscovery-is-not-working-on-android-10

同时也给APP增加了检查定位功能是否开启的功能,如果为开启,就跳转到对应的设置界面进行定位功能的开启,代码如下:

    private void openGPSSettings() {
        if (!checkGPSIsOpen() && Build.VERSION.SDK_INT >= 29) {
            //没有打开则弹出对话框
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("提示");
            dialog.setMessage("蓝牙连接功能需要开启定位辅助功能");
            dialog.setCancelable(false);//不能用返回关闭
            // 拒绝, 退出应用
            dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Activity activity=new Activity();
                    System.exit(0);
                }
            });

            dialog.setPositiveButton("开启定位",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //跳转GPS设置界面
                            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(intent);
                        }
                    });
            dialog.show();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号