简体   繁体   English

Android 6.0蓝牙程序无法发现任何可用的蓝牙设备

[英]Android 6.0 Bluetooth Program Cannot Discover Any Available Bluetooth Devices

GOAL 目标

Discovering all the available bluetooth devices such as my iPad with bluetooth ON (Discoverable). 发现所有可用的蓝牙设备,例如我的iPad蓝牙ON(可发现)。

ANDROID VERSION ANDROID VERSION

6.0 6

PROBLEM 问题

Cannot Discover any bluetooth devices. 无法发现任何蓝牙设备。

CODE

// Permission
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

public BroadcastReceiver mReceiver;
public IntentFilter filter;

private boolean discover_AvailableBluetoothDevice() {
        mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                Log.d(TAG, "onReceive Called");
                String action = intent.getAction();
                // When discovery finds a device
                if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                    Log.d(TAG, "ACTION_DISCOVERY_STARTED");
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    Log.d(TAG, "ACTION_DISCOVERY_FINISHED");
                } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    Log.d(TAG, "ACTION_FOUND");
                    // Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    // Add the name and address to an array adapter to show in a ListView
                    String str = device.getName() + "\n( " + device.getAddress() + " )";
                    adapter.add(str);
                }
            }
        };

        filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mReceiver, filter);
        boolean isSuccessDiscovery = mBluetoothAdapter.startDiscovery();
        return isSuccessDiscovery;
    }



EXECUTION RESULT (logcat) 执行结果(logcat)

02-02 01:17:26.142 7194-7194/eie.imt.smartswitch D/†MainActivity: This device support Bluetooth.
02-02 01:17:55.052 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: isSuccessDiscovery=true
02-02 01:17:55.147 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: onReceive Called
02-02 01:17:55.147 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: ACTION_DISCOVERY_STARTED
02-02 01:18:07.909 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: onReceive Called
02-02 01:18:07.910 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: ACTION_DISCOVERY_FINISHED

I see the program does not enter the condition block of ACTION_FOUND but my iPad's bluetooth is ON, where does the problem come from? 我看到程序没有进入ACTION_FOUND的条件块但我的iPad的蓝牙是ON,问题来自哪里?

If you use Android 6, add one of the following permissions: 如果您使用的是Android 6,请添加以下权限之一:

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

And turn on the Location service (GPS): 并打开位置服务(GPS):

在此输入图像描述

And turn on the Location permission: 并启用位置权限:

在此输入图像描述

Google now requires ACCESS_FINE_LOCTION or ACCESS_COARCE_LOCATION permissions to be able to scan for Bluetooth or Wifi devices. Google现在需要ACCESS_FINE_LOCTIONACCESS_COARCE_LOCATION权限才能扫描蓝牙或Wifi设备。 It's weird behavior but Google says it's how it should work from now on. 这是一种奇怪的行为,但谷歌表示从现在开始应该如何运作。

Link to AndroidDev about this 链接到AndroidDev关于此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM