2011-10-25 63 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); 

所以现在用它来填充你的蓝牙设备列表视图...