2011-10-28 53 views
1

我刚开始学习Android开发。我正在编写一个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() === 
+0

不知道为什么你标记basic4android这个问题。如果你想用Basic4android实现它,你应该看到这个[manual](http://www.basic4ppc.com/android/help/serial.html#bluetoothadmin)。 – Erel

+0

对不起,我是Android新手,我以为Basic4android是一个获得有关基本android编程的答案的地方。我想用Java解决这个问题 – SyncMaster

回答

4

你应该有蓝牙和BLUETOOTH_ADMIN权限您的清单