BroadcastReceiver从未调用过

时间:2012-11-25 20:52:35

标签: android bluetooth broadcastreceiver

我正在尝试扫描蓝牙设备,但永远不会调用寄存器:

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

         }};

    }

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

2 个答案:

答案 0 :(得分:2)

尝试将广播接收器放在

之前
 bluetoothAdapter.startDiscovery();

因为你启动发现并且在你收听broadcastListener之后。

答案 1 :(得分:1)

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

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

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

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

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

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