【问题标题】:BroadcastReceiver never calledBroadcastReceiver 从未调用过
【发布时间】:2012-11-25 20:52:34
【问题描述】:

我正在尝试扫描蓝牙设备,但从未调用过寄存器:

 public class MainActivity extends Activity {


          ArrayList<String> arr_devices = new ArrayList<String>();
          ArrayList<String> arr_in_range = new ArrayList<String>();
          Button btn_devices;
          BluetoothAdapter bluetoothAdapter;
          IntentFilter filter;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

            btn_devices = (Button)findViewById(R.id.search);
            btn_devices.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {

                    BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
                    if (bluetooth != null)
                    {
                        if (bluetooth.isEnabled())
                        {
                            //this is called
                            bluetoothAdapter.startDiscovery();
                            Log.i("State", bluetoothAdapter.getState() + ""); //12 (STATE_ON)
                            Log.i("Discovery", bluetoothAdapter.isDiscovering() + "");  //FALSE

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

                           registerReceiver(myreceiver, filter);
                           //this is where the program stops. No more actions are performed
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "Bluetooth is not enabled!", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });


        }


        @Override
        protected void onDestroy() {

            super.onDestroy();
            unregisterReceiver(myreceiver);
        }


        final BroadcastReceiver myreceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
        //I placed a Log statement here but it doesn't appear in the logcat
           String action = intent.getAction();

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

         }};

    }

蓝牙已启用。我错过了什么?

【问题讨论】:

  • 使用 Log.v 语句确保蓝牙不为空,并且蓝牙在设备上显示为已启用。另外,打印出 startDiscovery 的值。
  • 我认为您需要将 myreceiver 声明放在 onCreate() 中。现在,您在生命周期方法之外实例化它,但仍在 Activity 中。我认为当你注册它时它会为空,所以看起来你没有收到回调。
  • @PearsonArtPhoto:我正在使用日志语句,但删除了它们以提供透明代码。
  • 那么它在哪里停止呢?你能用 cmets 编辑你的代码以显示它停止的位置吗?
  • 嗯。在调用 startDiscovery() 之后,我立即注销了 bluetoothAdapter.isDiscovering() 的值为 false。有任何想法吗?我在代码中添加了一些 cmets

标签: android bluetooth broadcastreceiver


【解决方案1】:

试着把你的广播接收器放在前面

 bluetoothAdapter.startDiscovery();

因为你开始发现并在你收听广播监听器之后。

【讨论】:

  • 有时蓝牙会停止工作。它不会强制关闭,但 bluetoothAdapter.isDiscovering() 的值返回 false。无论我是否重新启动应用程序,我都需要关闭并打开蓝牙才能使其再次工作。有什么想法吗?
  • startDiscovery 启动设备以搜索新的 BT 设备,但当它完成 isDiscovering 返回 false 时,您必须再次调用 startDiscovery。看文档developer.android.com/reference/android/bluetooth/…
  • 这个我很熟悉。但我一直按 btn_devices 值返回 false。如果你再看一下代码,你会看到 startDiscovery() 方法在 btn_devices onClickListener 中,所以应该重新开始发现。..
  • 抱歉,可以看看这个stackoverflow.com/questions/8837386/…
【解决方案2】:

为什么要设置两个蓝牙适配器?系统可能会混淆它们,你应该只需要一个。我建议您删除下面列出的行,并将所有引用从 bluetooth 更改为 bluetoothAdapter。

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

我怀疑正在发生的事情是返回 null,因为您已经有了接收器的副本,或者类似的东西。

另外,请确保您已将权限添加到清单:

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

所有这些都失败了,您可以尝试重新启动手机。至少在我的手机上,蓝牙有时会坏掉。

【讨论】:

  • 我不小心留下了另一个蓝牙适配器。我按照你说的做了,但没有解决问题。
  • 好的,请确保您也将权限添加到清单中,因为我刚刚编辑了我的答案。
  • BLUETOOTH 和 BLUETOOTH_ADMIN 权限已添加,应用将在没有它们的情况下强制关闭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
相关资源
最近更新 更多