4

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

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

         }};

    }

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

4

2 回答 2

2

尝试将您的广播接收器放在之前

 bluetoothAdapter.startDiscovery();

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

于 2012-11-25T22:55:52.327 回答
1

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

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

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

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

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

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

于 2012-11-25T21:03:32.523 回答