Android Marshmallow 6.0.1 蓝牙扫描未返回任何结果

标签 android bluetooth sdk

根据 Kitkat 4.4.4 的更新 + appCompat,6.01 中的蓝牙似乎无法按预期使用以下代码和权限。

没有返回任何结果,我在附近有几个可发现的设备。

有人知道为什么吗?我在 Nexus 5 上运行。

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

package bluetoothscanneractivity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class BluetoothScannerActivity extends Activity {
    //private final BroadcastReceiver FoundReceiver = null;
    protected ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>();
    private ListView foundDevicesListView;
    private ArrayAdapter<BluetoothDevice> btArrayAdapter;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_scanner);
        final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
        final Button scanb = (Button) findViewById(R.id.button);
        final ListView foundDevicesListView = (ListView) findViewById(R.id.listView1);

        btArrayAdapter = new ArrayAdapter<BluetoothDevice>(this,
                android.R.layout.simple_list_item_1, foundDevices);
        foundDevicesListView.setAdapter(btArrayAdapter);

        //Turn on Bluetooth
        if (myBlueToothAdapter == null)
            Toast.makeText(BluetoothScannerActivity.this, "Your device doesnot support Bluetooth", Toast.LENGTH_LONG).show();
        else if (!myBlueToothAdapter.isEnabled()) {
            Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(BtIntent, 0);
            Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show();
        }
        //scan
        scanb.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                btArrayAdapter.clear();
                myBlueToothAdapter.startDiscovery();
                Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show();

            }
        });

        registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
        IntentFilter filter = new IntentFilter(
                BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(FoundReceiver, filter);

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterReceiver(FoundReceiver);
    }

    private final BroadcastReceiver FoundReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            // When discovery finds a new device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (!foundDevices.contains(device)) {
                    foundDevices.add(device);
                    btArrayAdapter.notifyDataSetChanged();
                }
            }

            // When discovery cycle finished
            if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                if (foundDevices == null || foundDevices.isEmpty()) {
                    Toast.makeText(BluetoothScannerActivity.this, "No Devices", Toast.LENGTH_LONG).show();
                }
            }

        }
    };


}

最佳答案

您知道自从 Marshmallow 以来,您的任务需要这些权限 -

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

此外,自 Marshmallow 以来,您必须以编程方式请求权限,即使您已在 Manifest 文件中声明它们也是如此。

因此您必须在startDiscovery()

之前请求位置权限
 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number

并且在用户接受这些权限后,就可以startDiscovery()了。如果他/她拒绝,您将无法发现这些设备。 您可以在

中检查用户操作

onRequestPermissionsResult() 回调。

关于Android Marshmallow 6.0.1 蓝牙扫描未返回任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36163751/

相关文章:

java - 如何用Java制作一个Android倒计时器?

Android 蓝牙聊天示例应用程序 - 多个连接

objective-c - 10.9 CoreBluetooth 检索外围设备

ios - 如何在 iOS 应用程序中验证 Aadhar 号码?

sdk - Dynamics CRM 使用 C# 代码合并两个联系人,从 SDK 修改示例

Android SDK Ubuntu 默认路径

java - 用退格键替换换行符到上一行

java - 如何在全局变量的回调中存储数据?

android - 单选按钮单击并重新单击

ios - Xamarin-IOS BTLE WroteCharacteristicValue 未触发