简体   繁体   English

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

[英]Discovering available bluetooth devices in android

I just started learning Android development. 我刚开始学习Android开发。 I am writing an Android program to scan available bluetooth devices and list them in a log file. 我正在编写一个Android程序来扫描可用的蓝牙设备并将它们列在日志文件中。 As I am new to Android, I am unable to figure out what is wrong in the below code snippet. 由于我是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();
            }
        });

When I try the debug mode, the control skips the " BroadcastReceiver() ". 当我尝试调试模式时,控件跳过“ BroadcastReceiver() ”。 I could not find any entries in the log. 我在日志中找不到任何条目。 Can you help me in figuring out what the problem is and how I can list the available bluetooth devices. 你能帮我搞清楚问题是什么以及如何列出可用的蓝牙设备。

[edit] I am getting error that the application has stopped unexpectedly: [edit]我收到应用程序意外停止的错误:

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() ===

您应该在清单中具有BLUETOOTH和BLUETOOTH_ADMIN权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM