Android 6.0 - 蓝牙 - Action_Found 广播意图不存在代码 [英] Android 6.0 - Bluetooth - No code exists for Action_Found broadcast intent

查看:22
本文介绍了Android 6.0 - 蓝牙 - Action_Found 广播意图不存在代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

我尝试了很多代码,也来自互联网上显示的示例.他们每个人都遵循我的方法.经过几个小时的测试,我得出的结论是,在Android 6.0上没有机会实现未知设备的蓝牙发现,我们只能检索绑定的设备.我很确定这个 android 版本有一些东西.

I tried many codes, also from examples shown on the internet. each of them follows my approach. After many hours of testing, i came to the conclusion that on Android 6.0 there's no chance to achieve bluetooth discovery of unknown devices, we can only retrieve the bonded ones. I'm pretty sure there's something with this android version.

如果有人知道如何解决这个问题,我将非常感谢您的帮助.

if someone knows how to fix this, i would really appreciate any help.

原帖

我的代码运行良好,但没有找到设备.我只收到 DISCOVERY_STARTED 和 DISCOVERY_FINISHED,所以没有找到设备,但是使用系统应用找到了这些设备.

My code is working fine, but no devices get found. i only receive DISCOVERY_STARTED and DISCOVERY_FINISHED, so no devices are found, but using system app these devices get found.

这是我的应用程序代码,希望能帮到你.

This is the code of my application, hope it can help.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();

//other stuff...

    IntentFilter filter=new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

    registerReceiver(myreceiver,filter);
}

final BroadcastReceiver myreceiver = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        Log.i("test","RECEIVED: "+ action);
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
        }

        if(BluetoothDevice.ACTION_FOUND.equals(action))
        {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.i("test", device.getName() + "
" + device.getAddress());
        }
    }};

public void scanDevices(View v){

        if (bluetoothAdapter.isEnabled()){

            bluetoothAdapter.startDiscovery();
        }
}

我已经设置了权限:

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

推荐答案

非常简单的解决方案:

1.向清单添加 FINE_LOCATION 权限:

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

2.在运行时请求 FINE_LOCATION 权限:

//since i was working with appcompat, i used ActivityCompat method, but this method can be called easily from Activity subclassess.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSION_REQUEST_CONSTANT);

3.实现 onRequestPermissionsResult 方法:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

switch (requestCode) {
    case MY_PERMISSION_REQUEST_CONSTANT: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            //permission granted!
        }
        return;
    }
}
}

这一切都是因为 Marshmallows 需要此权限才能使用蓝牙进行发现.

All this because Marshmallows requires this permission in order to use bluetooth for discovery.

由于这个权限属于危险权限组,简单地在manifest中声明它是行不通的,我们需要用户的明确同意才能使用该职位(即使我们不需要实际位置).

Since this permission belongs to the Dangerous group of permissions, simply declaring it in the manifest doesn't work, we need the user's explicit consent to use the position (even if we don't need the position actually).

这篇关于Android 6.0 - 蓝牙 - Action_Found 广播意图不存在代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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