2011-10-25 67 views
0

我想掃描可用的藍牙設備並將它們顯示在列表中。我是新手和發展的新手。我想ArrayAdapter存在一些問題,但無法找出確切的問題。在Eclipse中掃描可用於Android的藍牙設備

當我運行該程序時,我得到一個異常The application stops unexpectedly。你能幫我解決這個問題嗎?

 button2.setOnClickListener(new View.OnClickListener() 
      { 
       @SuppressWarnings("null") 
       public void onClick(View v) 
       { 
        ListView lv1 = (ListView) findViewById(R.id.myListView1); 
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
        // TODO Auto-generated method stub 

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
        // If there are paired devices 
        if (pairedDevices.size() > 0) 
        { 
         // Loop through paired devices 
         for (BluetoothDevice device : pairedDevices) 
         { 
          ArrayAdapter<String> mArrayAdapter = null; 
          // Add the name and address to an array adapter to show in a ListView 
          mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
          /*final ArrayList<String> btDevices = new ArrayList<String>(); 
          ArrayAdapter<String> aa = new ArrayAdapter<String>(CalcActivity.this, 
            android.R.layout.simple_list_item_1, 
            btDevices); 
*/ 
          lv1.setAdapter(mArrayAdapter); 
         } 
        } 
       } 
      }); 

我在mArrayAdapter.add(device.getName() + "\n" + device.getAddress());

編輯變得異常: 忍住了我的代碼,但我仍不能找出問題。

Discovering available bluetooth devices in android

+0

發佈您的LogCat輸出。 – Reno

回答

0

請確保您已包含在您的清單文件中BluetoothBluetooth_Admin權限。

0

你的錯誤,我想是這樣的是,arrayadapter尚未初始化 所以初始化(分配給它的arrayadapter一個一個有效參考)這樣

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,android.R.id.text1,list_names); 

所以現在用它來填充你的藍牙設備列表視圖...