安卓棉花糖6.0.1蓝牙扫描无结果

9

在6.01中,根据更新和适用于Kitkat 4.4.4的appCompat,蓝牙似乎无法按预期工作。尽管我周围有多个可发现设备,但没有返回结果。

有人知道原因吗?我正在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();
                }
            }

        }
    };


}

请问您能否粘贴startDiscovery()方法的代码? - Shadab Ansari
不需要 - 这是标准的Android BL调用。 - mcdoomington
你能看到Toast吗?-- Toast.makeText(BluetoothScannerActivity.this, "正在扫描设备", Toast.LENGTH_LONG).show(); - Shadab Ansari
是的 - 提示框正在工作,包括没有结果的提示框。 - mcdoomington
3个回答

19

你知道从Marshmallow开始,你需要为你的任务获取这些权限 -

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

自从Marshmallow以来,即使您已在清单文件中声明权限,也必须通过编程方式请求权限。

因此,在您使用startDiscovery()之前,您需要请求定位权限。

 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number

当用户接受这些权限之后,你可以调用startDiscovery()。如果用户拒绝了这些权限,你将无法发现设备。

你可以在onRequestPermissionsResult()回调中检查用户的操作。


啊,我不知道我需要编程请求权限 - 我会检查一下。 - mcdoomington
啊,我不知道我需要通过编程方式请求权限,但它仍然无法工作。我甚至通过应用程序信息权限页面启用了权限。 - mcdoomington
我可以确认这个修复了我的问题,我在我的主活动上实现它。 - Dayan
这个很好用;只需要将活动名称作为请求权限的第一个参数即可。 - GRosay

6

通过添加权限,将最低SDK版本更改为23 - 它可以与以下内容一起使用:

package bluetoothscanneractivity;

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.support.v7.app.AppCompatActivity;
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 android.Manifest;

import java.util.ArrayList;

public class BluetoothScannerActivity extends AppCompatActivity {
    //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 doesnt 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();
        }

        // Quick permission check
        int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
        permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
        if (permissionCheck != 0) {

            this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
        }


        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);
                    Toast.makeText(BluetoothScannerActivity.this, "name: " + device.getName() + " " + device.getAddress(), Toast.LENGTH_LONG).show();
                    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();
                }
            }

        }
    };


}

我不是应用程序开发人员,但我注意到自从我的手机升级到Android 6.0.1后,我的“blueterm”应用程序无法连接到我的嵌入式设备。这是否是一个类似的应用程序会遇到的问题? - Ed.
我怀疑是这样的 - 你可以修改权限处理,因为这只是一个快速而粗略的运行。 - mcdoomington
谢谢,我认为这需要我能够访问“blueterm”应用程序的源代码,对吗? - Ed.
它对我起作用。在搜索设备时要求用户提供其精细/粗略位置有点奇怪。 - severin.julien

2

如果SDK版本>LOLLIPOP,则仅执行以下方法。当我尝试在没有此限制的情况下使用它时,我的应用程序崩溃了。在使用.startDiscovery()之前,请简单地调用此方法。

public void checkBTPermissions(){
    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
            int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
        permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
        if (permissionCheck != 0) {

            this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
        }
    }else{
        Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP.");
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接