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

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

问题描述

更新

我试过很多codeS,还从互联网上显示的例子。他们每个人都遵循我的做法。几个小时的测试后,我来到了在Android 6.0有没有机会实现未知设备的蓝牙发现,我们只能检索保税的人的结论。
我是pretty肯定有什么东西在这个Android版本。

如果有人知道如何解决这个问题,我真的AP preciate任何帮助。


原贴

我的code是工作的罚款,但没有得到设备发现。
我只收到DISCOVERY_STARTED和DISCOVERY_FINISHED,所以没有设备被找到,但使用应用程式系统这些设备得到发现。

这是我的应用程序的code,希望能有所帮助。

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//其他的东西...    IntentFilter的过滤器=新的IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);    registerReceiver(myreceiver,过滤器);
}最终的BroadcastReceiver myreceiver =新的广播接收器(){    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        字符串行动= intent.getAction();        Log.i(测试,收到:+动作);
        如果(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(动作)){
        }
        否则如果(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(动作)){
        }        如果(BluetoothDevice.ACTION_FOUND.equals(动作))
        {
            BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.i(测试,device.getName()+\\ n+ device.getAddress());
        }
    }};公共无效scanDevices(视图v){        如果(bluetoothAdapter.isEnabled()){            bluetoothAdapter.startDiscovery();
        }
}

我已经得到了权限设置:

 <使用许可权的android:NAME =android.permission.BLUETOOTH/>
<使用许可权的android:NAME =android.permission.BLUETOOTH_ADMIN/>
<使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>


解决方案

很简单的解决办法:

1。添加FINE_LOCATION权限来体现:

 <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>

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

  //因为我与应用程序兼容性的工作,我用ActivityCompat方法,但这种方法可以轻松地从活动subclassess调用。
ActivityCompat.requestPermissions(这一点,新的String [] {} Manifest.permission.ACCESS_FINE_LOCATION,MY_PERMISSION_REQUEST_CONSTANT);

3。实现onRequestPermissionsResult方式:

 公共无效onRequestPermissionsResult(INT申请code,字符串权限[],INT [] grantResults){开关(要求code){
    案例MY_PERMISSION_REQUEST_CONSTANT:{
        //如果请求被取消,结果数组是空的。
        如果(grantResults.length大于0&放大器;&放大器; grantResults [0] == PackageManager.PERMISSION_GRANTED){            //许可授予!
        }
        返回;
    }
}
}

这一切都因为棉花糖需要这个权限才能使用蓝牙的发现。

由于此权限属于在危险组的权限,简单地宣布它在清单中不起作用,我们需要用户的明确同意使用位置(即使我们并不需要实际的位置)。

UPDATE

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.


Original Post

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() + "\n" + device.getAddress());
        }
    }};

public void scanDevices(View v){

        if (bluetoothAdapter.isEnabled()){

            bluetoothAdapter.startDiscovery();
        }
}

I already got permissions set:

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

解决方案

Very simple solution:

1. Add FINE_LOCATION permission to manifest:

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

2. Request FINE_LOCATION permission at runtime:

//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. Implement onRequestPermissionsResult method:

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

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

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 - 蓝牙 - 无code存在ACTION_FOUND广播意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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