简体   繁体   中英

Broadcastreceiver not working in android Nougat api level 25?

I have got HTC-M8 and installed nougat on it api level 25. The app works fine on other devices like samsung grand prime and samsung galaxy s4 (api level 22,23) , but unfortunately not working on M8 why? Main Activity

package com.example.idani.phoni;

import android.Manifest;
import android.app.ActionBar;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class Home extends AppCompatActivity {
    BluetoothAdapter bluetoothAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();


        IntentFilter inFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        BlueReciver bReciver = new BlueReciver();

        registerReceiver(bReciver,inFilter);

        bluetoothAdapter.startDiscovery();
    }
}

BlueReciver.java

package com.example.idani.phoni;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * Created by iDani on 6/28/2017.
 */

public class BlueReciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        BluetoothDevice dv = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Toast.makeText(context,"HI FOUND SOMETHING"+dv.getName(),Toast.LENGTH_LONG).show();
    }
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.idani.phoni">
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"

        >



        <activity android:name=".Home"

            >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Starting with API 23 (Marshmallow) there are two changes which affect your code:

  1. In order to receive BluetoothDevice.ACTION_FOUND your app must have one of the location permissions (eg ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION ).

  2. The permissions model changed in API 23, requiring runtime permission handling for dangerous level permissions, which both of the above location based permissions are.

So you'll need to add one of the location permissions to your manifest and also have to add runtime permission handling. Or, if you are not ready to support API 23+, set your target API version to 22 or lower.

Use these permissions in Android Manifest file. Your issues will be solved.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM