Android蓝牙startDiscovery无法正常工作

时间:2020-02-08 18:29:14

标签: android android-studio android-fragments kotlin bluetooth

我正在尝试学习如何在android中使用蓝牙。但是,我已经遇到了一个似乎找不到答案的问题。当我在蓝牙适配器上调用.startDiscovery()时,它返回false,表示失败。我没有做任何奇怪的事情,我使用BluetoothAdapter.getDefaultAdapter()获得了bluetoothAdapter,然后对其调用.startDiscovery()。 我正试图从一个片段中做到这一点。

ACCESS_FINE_LOCATION,BLUETOOTH_ADMIN和BLUETOOTH都在清单中

如果需要提供其他信息,请告诉我!

Logcat输出:

2020-02-08 19:24:54.373 16129-16129/? E/Zygote: isWhitelistProcess - Process is Whitelisted
    2020-02-08 19:24:54.376 16129-16129/? E/Zygote: accessInfo : 1
    2020-02-08 19:24:54.382 16129-16129/? I/.androidairpod: Late-enabling -Xcheck:jni
    2020-02-08 19:24:54.404 16129-16129/? E/.androidairpod: Unknown bits set in runtime_flags: 0x8000
    2020-02-08 19:24:54.424 16129-16129/? D/ActivityThread: setConscryptValidator
    2020-02-08 19:24:54.425 16129-16129/? D/ActivityThread: setConscryptValidator - put
    2020-02-08 19:24:54.477 16129-16129/com.lavenderr.bluetoothtest W/ActivityThread: Application com.lavenderr.bluetoothtest is waiting for the debugger on port 8100...
    2020-02-08 19:24:54.482 16129-16129/com.lavenderr.bluetoothtest I/System.out: Sending WAIT chunk
    2020-02-08 19:24:55.483 16129-16129/com.lavenderr.bluetoothtest I/System.out: Debugger has connected
    2020-02-08 19:24:55.484 16129-16129/com.lavenderr.bluetoothtest I/System.out: waiting for debugger to settle...
    2020-02-08 19:24:56.085 16129-16129/com.lavenderr.bluetoothtest I/chatty: uid=10416(com.lavenderr.bluetoothtest) identical 3 lines
    2020-02-08 19:24:56.286 16129-16129/com.lavenderr.bluetoothtest I/System.out: waiting for debugger to settle...
    2020-02-08 19:24:56.486 16129-16129/com.lavenderr.bluetoothtest I/System.out: waiting for debugger to settle...
    2020-02-08 19:24:56.687 16129-16129/com.lavenderr.bluetoothtest I/chatty: uid=10416(com.lavenderr.bluetoothtest) identical 1 line
    2020-02-08 19:24:56.887 16129-16129/com.lavenderr.bluetoothtest I/System.out: waiting for debugger to settle...
    2020-02-08 19:24:57.088 16129-16129/com.lavenderr.bluetoothtest I/System.out: debugger has settled (1305)
    2020-02-08 19:24:57.373 16129-16177/com.lavenderr.bluetoothtest D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
    2020-02-08 19:24:57.485 16129-16129/com.lavenderr.bluetoothtest I/MultiWindowDecorSupport: updateCaptionType >> DecorView@a5bf5f5[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
    2020-02-08 19:24:57.486 16129-16129/com.lavenderr.bluetoothtest D/MultiWindowDecorSupport: setCaptionType = 0, DecorView = DecorView@a5bf5f5[]
    2020-02-08 19:24:57.546 16129-16129/com.lavenderr.bluetoothtest W/.androidairpod: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
    2020-02-08 19:24:57.549 16129-16129/com.lavenderr.bluetoothtest W/.androidairpod: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
    2020-02-08 19:24:57.842 16129-16129/com.lavenderr.bluetoothtest W/.androidairpod: Verification of void com.lavenderr.bluetoothtest.ui.HomeFragment.<init>() took 122.221ms
    2020-02-08 19:25:07.916 16129-16129/com.lavenderr.bluetoothtest D/BluetoothAdapter: startDiscovery
    2020-02-08 19:25:17.880 16129-16129/com.lavenderr.bluetoothtest D/BluetoothAdapter: startDiscovery
    2020-02-08 19:25:18.088 16129-16129/com.lavenderr.bluetoothtest D/ViewRootImpl@b75c09d[MainActivity]: setView = com.android.internal.policy.DecorView@a5bf5f5 TM=true MM=false
    2020-02-08 19:25:18.235 16129-16129/com.lavenderr.bluetoothtest D/ViewRootImpl@b75c09d[MainActivity]: Relayout returned: old=(0,0,1080,2220) new=(0,0,1080,2220) req=(1080,2220)0 dur=15 res=0x7 s={true 546482515968} ch=true
    2020-02-08 19:25:18.237 16129-16175/com.lavenderr.bluetoothtest D/OpenGLRenderer: createReliableSurface : 0x7f3cea3980, 0x7f3ce57000
    2020-02-08 19:25:18.253 16129-16175/com.lavenderr.bluetoothtest D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
    2020-02-08 19:25:18.313 16129-16175/com.lavenderr.bluetoothtest W/Gralloc3: mapper 3.x is not supported
    2020-02-08 19:25:18.317 16129-16175/com.lavenderr.bluetoothtest I/gralloc: Arm Module v1.0
    2020-02-08 19:25:18.453 16129-16129/com.lavenderr.bluetoothtest D/ViewRootImpl@b75c09d[MainActivity]: MSG_RESIZED_REPORT: frame=(0,0,1080,2220) ci=(0,72,0,45) vi=(0,72,0,45) or=1
    2020-02-08 19:25:18.457 16129-16129/com.lavenderr.bluetoothtest D/ViewRootImpl@b75c09d[MainActivity]: stopped(false) old=false
    2020-02-08 19:25:18.468 16129-16129/com.lavenderr.bluetoothtest D/ViewRootImpl@b75c09d[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
    2020-02-08 19:25:18.470 16129-16129/com.lavenderr.bluetoothtest D/InputMethodManager: prepareNavigationBarInfo() DecorView@a5bf5f5[MainActivity]
    2020-02-08 19:25:18.470 16129-16129/com.lavenderr.bluetoothtest D/InputMethodManager: getNavigationBarColor() -855310
    2020-02-08 19:25:18.479 16129-16129/com.lavenderr.bluetoothtest D/InputMethodManager: prepareNavigationBarInfo() DecorView@a5bf5f5[MainActivity]
    2020-02-08 19:25:18.480 16129-16129/com.lavenderr.bluetoothtest D/InputMethodManager: getNavigationBarColor() -855310
    2020-02-08 19:25:18.480 16129-16129/com.lavenderr.bluetoothtest V/InputMethodManager: Starting input: tba=com.lavenderr.bluetoothtest ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
    2020-02-08 19:25:18.481 16129-16129/com.lavenderr.bluetoothtest D/InputMethodManager: startInputInner - Id : 0
    2020-02-08 19:25:18.481 16129-16129/com.lavenderr.bluetoothtest I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
    2020-02-08 19:25:18.542 16129-16129/com.lavenderr.bluetoothtest D/InputMethodManager: prepareNavigationBarInfo() DecorView@a5bf5f5[MainActivity]
    2020-02-08 19:25:18.543 16129-16129/com.lavenderr.bluetoothtest D/InputMethodManager: getNavigationBarColor() -855310
    2020-02-08 19:25:18.543 16129-16129/com.lavenderr.bluetoothtest V/InputMethodManager: Starting input: tba=com.lavenderr.bluetoothtest ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
    2020-02-08 19:25:18.544 16129-16129/com.lavenderr.bluetoothtest D/InputMethodManager: startInputInner - Id : 0

2 个答案:

答案 0 :(得分:0)

经过长时间的搜索,我终于找到了答案。即使我的清单中有ACCESS_FINE_LOCATION权限,我仍然需要“手动”请求该权限。我是通过以下方式完成的:

if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_FINE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {

    Log.i("info", "No fine location permissions")

    ActivityCompat.requestPermissions(this,
        arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
        1)
}

执行这段代码后,因此获得了启用发现的正确权限,我的问题就解决了!

答案 1 :(得分:0)

同样的问题发生在我的Visual Studio 2019 Xamarin项目的Android部分,C#代码有点不同:

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);
        this.ActionBar.SetIcon(Android.Resource.Color.Transparent);
        LoadApplication(new App());

        GetAccessFineLocationPermission();
    }

    private void GetAccessFineLocationPermission()
    {
        if (ContextCompat.CheckSelfPermission(this,Manifest.Permission.AccessFineLocation)!=Permission.Granted)
        {
            ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.AccessFineLocation},1);
        }
    }