在Android中发现可用的蓝牙设备

时间:2011-10-28 03:25:20

标签: android eclipse bluetooth

我刚开始学习Android开发。我正在编写一个Android程序来扫描可用的蓝牙设备并将它们列在日志文件中。由于我是Android新手,我无法弄清楚以下代码段中的错误。

 button2.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                ListView lv1 = (ListView) findViewById(R.id.myListView1);
                BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();


                final BroadcastReceiver mReceiver = new BroadcastReceiver() 
                { 
                    public void onReceive(Context context, Intent intent) 
                    {
                        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);
                        Log.v("BlueTooth Testing",device.getName() + "\n"
                        + device.getAddress()); 
                        }
                    }    
                };

                String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
                startActivityForResult(new Intent(aDiscoverable),DISCOVERY_REQUEST);
                IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
                registerReceiver(mReceiver, filter); 
                mBluetoothAdapter.startDiscovery();
            }
        });

当我尝试调试模式时,控件会跳过“BroadcastReceiver()”。我在日志中找不到任何条目。你能帮我弄清问题是什么以及如何列出可用的蓝牙设备。

[编辑] 我收到应用程序意外停止的错误:

Logcat Errors:
10-28 20:08:24.201: ERROR/UpdateReceiver(914): ACTION_PACKAGE_REMOVED
10-28 20:08:28.415: ERROR/RequestPermissionActivity(431): Timeout = 120
10-28 20:08:44.291: ERROR/DTUN_HCID4(521): === dtunops_stop_discovery() ===

1 个答案:

答案 0 :(得分:4)

你的清单中应该有BLUETOOTH和BLUETOOTH_ADMIN权限