首页 文章

BroadcastReceiver 无法启动

提问于
浏览
0

我正在练习蓝牙应用程序,实际上我已经完成了诸如用户界面,打开,关闭,获取 MAC 地址和已保存设备名称等问题,问题是实际上我想找其他设备,问题是实际上是没有进入方法:

私人最终 BroadcastReceiver mReceive

//buscar nuevos
public void nuevo(View v) {

    if (bluetooth.isEnabled()) {

        // Register the BroadcastReceiver
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);

        Log.d(TAG, "SE HA FINALIZADO EXITOSAMENTE LA ETAPA UNO");

    } else {

        // bluetooth is off so can't get
        Toast.makeText(this, "Active primero el bluetooth", Toast.LENGTH_SHORT).show();

    }
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {

        Log.d(TAG, "INICIANDO LA ETAPA DOS");

        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // Add the name and address to an array adapter to show in a ListView
            txt2.append(device.getName() + "\n" + device.getAddress());

            Log.d(TAG, "ETAPA DOS FINALIZADA");
        }
    }
};

我真的是数小时和数小时。

2 回答

  • 0

    nuevo(View v)在哪里被召唤。除非调用方法,否则您的接收器未注册且无法工作。尝试在 onresume()中注册接收器。

  • 0

    我非常失望,经过 20 多个小时我只有 2 个答案,没有人能够绝对达到答案,我一直在搜索,直到我发现问题在权限中,在 Android 5.0 之后,设备需要检查再次执行时的权限,添加此行。

    ActivityCompat.requestPermissions(this,new String [1] {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},1001);

    之前

    bluetooth.startDiscovery();

    但我会留在这里,以避免将来有人像我一样试图找到问题超过 24 小时。

相关问题