2011-10-28 77 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權限您的清單