Android 蓝牙广播接收器重新启动空

Android 蓝牙广播接收器重新启动空,android,bluetooth,broadcastreceiver,Android,Bluetooth,Broadcastreceiver,我正在学习为Android编写代码,我的第一个项目使用蓝牙,所以我想从那里开始。基本上,我试图通过按下按钮扫描其他可用的蓝牙设备,并在文本视图中显示其名称 我用一个动作过滤器注册了一个广播接收器(mReceiver)。我知道我正在正确地开始发现,因为其他设备可以看到我,但我无法获取它们的名称。通过AS进行调试似乎表明我的mReceiver实例为null public class MainActivity extends AppCompatActivity { private Scanner_B

我正在学习为Android编写代码,我的第一个项目使用蓝牙,所以我想从那里开始。基本上,我试图通过按下按钮扫描其他可用的蓝牙设备,并在文本视图中显示其名称

我用一个动作过滤器注册了一个广播接收器(mReceiver)。我知道我正在正确地开始发现,因为其他设备可以看到我,但我无法获取它们的名称。通过AS进行调试似乎表明我的mReceiver实例为null

public class MainActivity extends AppCompatActivity {

private Scanner_Bluetooth mainBLScanner;
public BroadcastReceiver mReceiver;
public int DISCOVERY_REQUEST = 1;
public static final String[] runtimePermissions = {};

public static final int LOCATION_PERMISSION_IDENTIFIER = 1;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Button buttonConnect = (Button) findViewById(R.id.buttonBT);
    final TextView tv_name =(TextView)findViewById(R.id.tv_name);
    mainBLScanner = new Scanner_Bluetooth(this, 5000, -75);
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    mReceiver = new BroadcastReceiver(getApplicationContext());
    registerReceiver(mReceiver, filter);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mainBLScanner.Start();
            tv_name.setText(mReceiver.getDeviceName());
        }
    };
    buttonConnect.setOnClickListener(listener);


}

@Override
protected void onDestroy() {
    unregisterReceiver(mReceiver);
    super.onDestroy();
}

@Override
protected void onStop() {
    unregisterReceiver(mReceiver);
    super.onStop();
}
这就是我实现广播接收机的方式

public class BroadcastReceiver extends android.content.BroadcastReceiver {
private String deviceName = "DUMMY";
private String deviceHardwareAddress;
Context activityContext;
private static final String TAG = "BroadcastReceiver";

public BroadcastReceiver(Context activityContext) {
    this.activityContext = activityContext;
}

public String getDeviceName(){
    return deviceName;
}

public String getAddress(){
    return deviceHardwareAddress;
}
//this detects when bluetooth is on/off
@Override
public void onReceive(Context context, Intent intent) {


        final String sAction = intent.getAction();
    Log.d(TAG, "start onReceive: " + sAction);
    if (sAction.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

        switch (state) {
            case BluetoothAdapter.STATE_OFF:

                Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_LONG).show();
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_LONG).show();
                break;
            case BluetoothAdapter.STATE_ON:

                Toast.makeText(context, "Bluetooth is on", Toast.LENGTH_LONG).show();
                break;
            case BluetoothAdapter.STATE_TURNING_ON:

                Toast.makeText(context, "Bluetooth is turning on", Toast.LENGTH_LONG).show();
                break;

            default:
                break;
        }

        if (BluetoothDevice.ACTION_FOUND.equals(sAction)) {
            Log.d(TAG, " Action_Found");
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(context,"Device Found!!",Toast.LENGTH_LONG).show();
            this.deviceName = device.getName();
            this.deviceHardwareAddress = device.getAddress(); // MAC address

        }

    }

}}

在我的清单中,我同时拥有Bluetooth和BluetoothAdmin权限。还有粗略和精细访问。

除了权限之外,您是否在清单中注册了android.bluetooth.adapter.action.STATE的意向过滤器?是的,除了我使用了
,因为我的第一个目标是获取发现的设备的名称