蓝牙发现无法正常工作

时间:2018-05-16 16:23:36

标签: android bluetooth

我是android新手。 我正在尝试一个简单的Android应用程序,它将: - 用户单击按钮启用BT - 用户单击按钮,它将扫描附近的设备并提供列表或尝试连接到特定设备(应用程序将用于机器人) 安全按钮工作正常并启用蓝牙,但是当我按下扫描按钮时它启动发现但没有返回回叫。我已经设置了一个监听器,并在那里放了一些日志,我没有看到任何来自监听器的东西(我确信在测试时有可发现的设备) 以下是代码:

    05-16 17:10:16.738 3072-3289/com.example.test D/MALI: eglCreateImageKHR:511: [Crop] 0 0 1856 2048  img[1856 2048] 
    05-16 17:10:16.740 3072-3289/com.example.test D/Surface: Surface::connect(this=0x7f8905f000,api=1)
    05-16 17:10:16.741 3072-3289/com.example.test W/libEGL: [ANDROID_RECORDABLE] format: 1
    05-16 17:10:16.742 3072-3289/com.example.test D/mali_winsys: new_window_surface returns 0x3000
    05-16 17:10:16.758 3072-3072/com.example.test W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
    05-16 17:10:16.788 3072-3289/com.example.test I/PerfService: PerfServiceNative api init
    05-16 17:10:16.795 3072-3289/com.example.test D/OpenGLRenderer: CacheTexture 4 upload: x, y, width height = 0, 0, 105, 164
    05-16 17:10:16.801 3072-3289/com.example.test I/[MALI][Gralloc]: [+]r_hnd(0x7f87705aa0), client(50), share_fd(59)
    05-16 17:10:16.801 3072-3289/com.example.test D/GraphicBuffer: register, handle(0x7f87705aa0) (w:1080 h:1920 s:1088 f:0x1 u:0x000b00)
    05-16 17:10:16.806 3072-3289/com.example.test D/OpenGLRenderer: ProgramCache save to disk, size = 7
    05-16 17:10:16.815 3072-3289/com.example.test I/[MALI][Gralloc]: [+]r_hnd(0x7f87705a00), client(50), share_fd(62)
    05-16 17:10:16.815 3072-3289/com.example.test D/GraphicBuffer: register, handle(0x7f87705a00) (w:1080 h:1920 s:1088 f:0x1 u:0x000b00)
    05-16 17:10:16.818 3072-3072/com.example.test V/InputMethodManager: onWindowFocus: null softInputMode=288 first=true flags=#81810100
        START INPUT: com.android.internal.policy.PhoneWindow$DecorView{b390d2e V.E...... R.....ID 0,0-1080,1920} ic=null tba=android.view.inputmethod.EditorInfo@f310fac controlFlags=#104
    05-16 17:10:16.838 3072-3289/com.example.test I/[MALI][Gralloc]: [+]r_hnd(0x7f87705be0), client(50), share_fd(65)
    05-16 17:10:16.838 3072-3289/com.example.test D/GraphicBuffer: register, handle(0x7f87705be0) (w:1080 h:1920 s:1088 f:0x1 u:0x000b00)
    05-16 17:10:20.866 3072-3289/com.example.test I/[MALI][Gralloc]: [+]r_hnd(0x7f87706040), client(50), share_fd(67)
    05-16 17:10:20.866 3072-3289/com.example.test D/GraphicBuffer: register, handle(0x7f87706040) (w:1080 h:1920 s:1088 f:0x1 u:0x000b00)
    05-16 17:10:20.866 3072-3072/com.example.test D/ActivityThread: holder:android.app.IActivityManager$ContentProviderHolder@540fc0a, holder.provider:android.content.ContentProviderProxy@3328c7b
    **05-16 17:10:20.867 3072-3072/com.example.test I/NKNKNK: TunrON button clicked**
    05-16 17:10:20.870 3072-3072/com.example.test D/BluetoothAdapter: isEnabled
    05-16 17:10:20.870 3072-3072/com.example.test I/Timeline: Timeline: Activity_launch_request time:263559951
    05-16 17:10:20.883 3072-3072/com.example.test D/BluetoothAdapter: getName
    05-16 17:10:20.889 3072-3072/com.example.test D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{4162956 token=android.os.BinderProxy@a3cc6c4 {com.example.test/com.example.test.MainActivity}}
    05-16 17:10:20.890 3072-3289/com.example.test D/OpenGLRenderer: CacheTexture 4 upload: x, y, width height = 28, 0, 120, 187
    05-16 17:10:21.659 3072-3072/com.example.test V/ActivityThread: Finishing stop of ActivityRecord{4162956 token=android.os.BinderProxy@a3cc6c4 {com.example.test/com.example.test.MainActivity}}: show=true win=com.android.internal.policy.MiuiPhoneWindow@5537557
    05-16 17:10:23.706 3072-3223/com.example.test D/BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@a93a844
    05-16 17:10:24.130 3072-3072/com.example.test V/ActivityThread: Performing resume of ActivityRecord{4162956 token=android.os.BinderProxy@a3cc6c4 {com.example.test/com.example.test.MainActivity}}
    05-16 17:10:24.131 3072-3072/com.example.test D/ActivityThread: ACT-AM_ON_RESUME_CALLED ActivityRecord{4162956 token=android.os.BinderProxy@a3cc6c4 {com.example.test/com.example.test.MainActivity}}
    05-16 17:10:24.131 3072-3072/com.example.test V/ActivityThread: Resume ActivityRecord{4162956 token=android.os.BinderProxy@a3cc6c4 {com.example.test/com.example.test.MainActivity}} started activity: false, hideForNow: false, finished: false
        Resuming ActivityRecord{4162956 token=android.os.BinderProxy@a3cc6c4 {com.example.test/com.example.test.MainActivity}} with isForward=false
    05-16 17:10:24.132 3072-3072/com.example.test V/PhoneWindow: DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{c8ed89e com.example.test/com.example.test.MainActivity,ident = 0}, this =com.android.internal.policy.PhoneWindow$DecorView{b390d2e V.E...... R....... 0,0-1080,1920}
    05-16 17:10:24.132 3072-3072/com.example.test V/ActivityThread: Scheduling idle handler for ActivityRecord{4162956 token=android.os.BinderProxy@a3cc6c4 {com.example.test/com.example.test.MainActivity}}
    05-16 17:10:24.141 3072-3072/com.example.test V/InputMethodManager: onWindowFocus: null softInputMode=32 first=false flags=#81810100
        START INPUT: com.android.internal.policy.PhoneWindow$DecorView{b390d2e V.E...... R......D 0,0-1080,1920} ic=null tba=android.view.inputmethod.EditorInfo@5ce2ae controlFlags=#100
    05-16 17:10:28.904 3072-3072/com.example.test D/SettingsInterface:  from settings cache , name = sound_effects_enabled , value = 0
    **05-16 17:10:28.904 3072-3072/com.example.test I/NKNKNK: SCAN button clicked
    05-16 17:10:28.904 3072-3072/com.example.test D/BluetoothAdapter: isEnabled
    05-16 17:10:28.906 3072-3072/com.example.test D/BluetoothAdapter: isDiscovering
    05-16 17:10:28.908 3072-3072/com.example.test D/BluetoothAdapter: 141409244: getState(). Returning 12
    05-16 17:10:28.912 3072-3072/com.example.test I/NKNKNK: Starting discovery logged
    05-16 17:10:28.913 3072-3072/com.example.test D/BluetoothAdapter: startDiscovery
    05-16 17:10:28.914 3072-3072/com.example.test D/BluetoothAdapter: 141409244: getState(). Returning 12**

以及我在Android studio中获得的日志:

$rootscope

编辑:我正在使用redmi note 4来运行该应用 感谢

1 个答案:

答案 0 :(得分:0)

您好我找到了我的问题的解决方案,似乎从Android 6您必须要求许可,而不仅仅是在清单中声明它们。所以我必须在启用BT之前添加以下行:

    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        Log.i("NKNKNK","Permission is not granted");
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    }