Android的棉花糖6.0.1蓝牙扫描没有返回结果 [英] Android Marshmallow 6.0.1 Bluetooth Scan Returning No Results

查看:419
本文介绍了Android的棉花糖6.0.1蓝牙扫描没有返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

蓝牙6.01似乎具有以下code和权限每次更新+应用程序兼容性的奇巧4.4.4预计将无法正常工作。

无结果被返回,我在附近的几个发现的设备。

任何人有任何见解按为什么呢?我正在上的Nexus 5。

 <使用许可权的android:NAME =android.permission.BLUETOOTH_ADMIN/>
    <使用许可权的android:NAME =android.permission.BLUETOOTH/>
    <使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>包bluetoothscanneractivity;进口android.app.Activity;
进口android.bluetooth.BluetoothAdapter;
进口android.bluetooth.BluetoothDevice;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.Toast;进口的java.util.ArrayList;公共类BluetoothScannerActivity延伸活动{
    //私人最终的BroadcastReceiver FoundReceiver = NULL;
    保护的ArrayList<&BluetoothDevice类GT; foundDevices =新的ArrayList<&BluetoothDevice类GT;();
    私人的ListView foundDevicesListView;
    私人ArrayAdapter<&BluetoothDevice类GT; btArrayAdapter;    / **
     * 在第一次创建活动时调用。
     * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_bluetooth_scanner);
        最终BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
        最终按钮scanb =(按钮)findViewById(R.id.button);
        最终的ListView foundDevicesListView =(ListView控件)findViewById(R.id.listView1);        btArrayAdapter =新ArrayAdapter<&BluetoothDevice类GT;(这一点,
                android.R.layout.simple_list_item_1,foundDevices);
        foundDevicesListView.setAdapter(btArrayAdapter);        //开启蓝牙
        如果(myBlueToothAdapter == NULL)
            Toast.makeText(BluetoothScannerActivity.this,您的设备不列入支持蓝牙,Toast.LENGTH_LONG).show();
        否则如果(!myBlueToothAdapter.isEnabled()){
            意图BtIntent =新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(BtIntent,0);
            Toast.makeText(BluetoothScannerActivity.this,打开蓝牙,Toast.LENGTH_LONG).show();
        }
        //扫描
        scanb.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                btArrayAdapter.clear();
                myBlueToothAdapter.startDiscovery();
                Toast.makeText(BluetoothScannerActivity.this扫描设备,Toast.LENGTH_LONG).show();            }
        });        registerReceiver(FoundReceiver,新的IntentFilter(BluetoothDevice.ACTION_FOUND));
        IntentFilter的过滤器=新的IntentFilter(
                BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(FoundReceiver,过滤器);    }    @覆盖
    保护无效的onDestroy(){
        // TODO自动生成方法存根
        super.onDestroy();
        unregisterReceiver(FoundReceiver);
    }    私人最终的BroadcastReceiver FoundReceiver =新的广播接收器(){
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){            字符串行动= intent.getAction();            //当发现找到一个新设备
            如果(BluetoothDevice.ACTION_FOUND.equals(动作)){
                //从意向获取BluetoothDevice类对象
                BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                如果(!foundDevices.contains(设备)){
                    foundDevices.add(设备);
                    btArrayAdapter.notifyDataSetChanged();
                }
            }            //当发现周期完成
            如果(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(动作)){
                如果(foundDevices == NULL || foundDevices.isEmpty()){
                    Toast.makeText(BluetoothScannerActivity.this,无设备,Toast.LENGTH_LONG).show();
                }
            }        }
    };
}


解决方案

通过添加权限,改变到最小SDK版本为23 - 它适用于以下内容:

 包bluetoothscanneractivity;进口android.bluetooth.BluetoothAdapter;
进口android.bluetooth.BluetoothDevice;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.os.Bundle;
进口android.support.v7.app.AppCompatActivity;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.Toast;
进口android.Manifest;进口的java.util.ArrayList;公共类BluetoothScannerActivity扩展AppCompatActivity {
    //私人最终的BroadcastReceiver FoundReceiver = NULL;
    保护的ArrayList<&BluetoothDevice类GT; foundDevices =新的ArrayList<&BluetoothDevice类GT;();
    私人的ListView foundDevicesListView;
    私人ArrayAdapter<&BluetoothDevice类GT; btArrayAdapter;    / **
     * 在第一次创建活动时调用。
     * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_bluetooth_scanner);
        最终BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();        最终按钮scanb =(按钮)findViewById(R.id.button);
        最终的ListView foundDevicesListView =(ListView控件)findViewById(R.id.listView1);        btArrayAdapter =新ArrayAdapter<&BluetoothDevice类GT;(这一点,
                android.R.layout.simple_list_item_1,foundDevices);        foundDevicesListView.setAdapter(btArrayAdapter);        //开启蓝牙
        如果(myBlueToothAdapter == NULL)
            Toast.makeText(BluetoothScannerActivity.this,您的设备犯规支持蓝牙,Toast.LENGTH_LONG).show();
        否则如果(!myBlueToothAdapter.isEnabled()){
            意图BtIntent =新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(BtIntent,0);
            Toast.makeText(BluetoothScannerActivity.this,打开蓝牙,Toast.LENGTH_LONG).show();
        }        //快速权限检查
        INT的PermissionCheck = this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
        的PermissionCheck + = this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
        如果(的PermissionCheck!= 0){            this.requestPermissions(新的String [] {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},1001); //任意数量
        }
        scanb.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                btArrayAdapter.clear();                myBlueToothAdapter.startDiscovery();                Toast.makeText(BluetoothScannerActivity.this扫描设备,Toast.LENGTH_LONG).show();            }
        });        registerReceiver(FoundReceiver,新的IntentFilter(BluetoothDevice.ACTION_FOUND));
        IntentFilter的过滤器=新的IntentFilter(
                BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(FoundReceiver,过滤器);    }    @覆盖
    保护无效的onDestroy(){
        // TODO自动生成方法存根
        super.onDestroy();
        unregisterReceiver(FoundReceiver);
    }
    私人最终的BroadcastReceiver FoundReceiver =新的广播接收器(){
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){            字符串行动= intent.getAction();            //当发现找到一个新设备
            如果(BluetoothDevice.ACTION_FOUND.equals(动作)){
                //从意向获取BluetoothDevice类对象
                BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                如果(!foundDevices.contains(设备)){
                    foundDevices.add(设备);
                    Toast.makeText(BluetoothScannerActivity.this,姓名:+ device.getName()++ device.getAddress(),Toast.LENGTH_LONG).show();
                    btArrayAdapter.notifyDataSetChanged();
                }            }            //当发现周期完成
            如果(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(动作)){
                如果(foundDevices == NULL || foundDevices.isEmpty()){
                    Toast.makeText(BluetoothScannerActivity.this,无设备,Toast.LENGTH_LONG).show();
                }
            }        }
    };
}

Bluetooth in 6.01 seems to be not working as expected with the following code and permissions as per update + appCompat for Kitkat 4.4.4.

No results are being returned and I have several discoverable devices in the vicinity.

Anyone have any insight as per why? I am running on a 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();
                }
            }

        }
    };


}

解决方案

With the added permissions, changing to min SDk version to 23 - it works with the following:

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蓝牙扫描没有返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆