簡體   English   中英

Android 6.0藍牙程序無法發現任何可用的藍牙設備

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

目標

發現所有可用的藍牙設備,例如我的iPad藍牙ON(可發現)。

ANDROID VERSION

6

問題

無法發現任何藍牙設備。

// 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;
    }



執行結果(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

我看到程序沒有進入ACTION_FOUND的條件塊但我的iPad的藍牙是ON,問題來自哪里?

如果您使用的是Android 6,請添加以下權限之一:

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

並打開位置服務(GPS):

在此輸入圖像描述

並啟用位置權限:

在此輸入圖像描述

Google現在需要ACCESS_FINE_LOCTIONACCESS_COARCE_LOCATION權限才能掃描藍牙或Wifi設備。 這是一種奇怪的行為,但谷歌表示從現在開始應該如何運作。

鏈接到AndroidDev關於此

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM