2011-08-26 197 views
4

我正嘗試創建一個圖像按鈕,當按下該按鈕時,向用戶呈現配對的藍牙設備連接列表。尋找Android藍牙配對設備

但是,在## 1, 處得到「設置無法解析爲變量」,並且在## 2處出現「mArrayAdapber無法解析」 (## 1和## 2不是代碼...)

我使用了Android站點的代碼,但在黑暗中,我發現自己處於黑暗中。

我會很感激一些指導...

//搜索

ImageButton bSearch = (ImageButton) findViewById(R.id.Search); 
bSearch.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) {       
     ##1Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     // If there are paired devices 
     if (pairedDevices.size() > 0) { 
      // Loop through paired devices 
      for (BluetoothDevice device : pairedDevices) { 
       // Add the name and address to an array adapter to show in a ListView 
       ##2mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
      } 
     }                   
    }      
}); 
+0

它看起來像您根據您的描述有語法錯誤。第一個看起來像是缺少Set類的導入,另一個是你沒有定義mArrayAdapter。 – momo

+0

嘿,你能給我們的設備中的藍牙設備搜索完整的源代碼。提前致謝。 – anddev

回答

4

爲1),那麼,如果你還沒有這樣做,加

>進口java.util.Set中;

在您的進口報表。這將解決「設置」錯誤。

對於2)聲明並初始化

mArrayAdapter

例如,在您的活動做:

private ArrayAdapter<String> mArrayAdapter; 

再上的onCreate:

mArrayAdapter= new ArrayAdapter<String>(this, <your layout file>); 

那麼應該添加到ListView

//查找並設置ListView控件爲新發現的設備

ListView newDevicesListView = (ListView) 
findViewById(R.id.<layout_file>); 
     newDevicesListView.setAdapter(mArrayAdapter); 

newDevicesListView.setOnItemClickListener(mDeviceClickListener); 

參考藍牙聊天來自Android例子的例子。它應該可以幫助您與藍牙API的


更新的評論會:

如果您在BluetoothChat密切關注。在BT例如Java文件,你會看到這個

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(D) Log.d(TAG, "onActivityResult " + resultCode); 
     switch (requestCode) { 
     case REQUEST_CONNECT_DEVICE: 
      // When DeviceListActivity returns with a device to connect 
      if (resultCode == Activity.RESULT_OK) { 
       // Get the device MAC address 
       String address = data.getExtras() 
            .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); 
       // Get the BLuetoothDevice object 
       BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 
       // Attempt to connect to the device 
       mChatService.connect(device); 
      } 
      break; 
     case REQUEST_ENABLE_BT: 
      // When the request to enable Bluetooth returns 
      if (resultCode == Activity.RESULT_OK) { 
       // Bluetooth is now enabled, so set up a chat session 
       setupChat(); 
      } else { 
       // User did not enable Bluetooth or an error occured 
       Log.d(TAG, "BT not enabled"); 
       Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 
     } 
    } 

關注此線:

// Attempt to connect to the device 
mChatService.connect(device); 

此功能連接到藍牙設備。第一次它會要求你自動配對。一旦配對,下次它會自動連接到藍牙設備。

+0

感謝您的回覆。我很感激幫助。 – Belboz

+0

我檢查了藍牙聊天的例子,並且很難遵循。沒有提供用於配對的設備搜索和選擇設備的按鈕。有不同的例子嗎? – Belboz

+0

我已經更新了我的答案...... –

0

分別僅有從##1Set<BluetoothDevice>##2mArrayAdapter您的代碼刪除##1##2。我想你只是從其他來源複製/粘貼,並沒有注意。這不是原始代碼的一部分。它只是用於列表編號的目的。

+0

是的,我只是將它包括在內以顯示我遇到問題的位置。 – Belboz

+1

好吧,對於任何人看我的問題作爲參考。我發現解決我的問題的最佳方法是發現我可以創建一個按鈕,打開藍牙設置屏幕。我沒有意識到你可以做到這一點,否則我會從一開始。 .................................................. ................................. startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)); – Belboz

3

嗨,你也可以嘗試這個代碼,你剛剛得到的設備綁定設備。

private ArrayAdapter<String> bondedAdapter = null; 
    private ListView listViewPairedDevices = null; 

@Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 
     try { 
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    listViewPairedDevices = (ListView) findViewById(R.id.listViewPairedDevices); 
    bondedAdapter = new ArrayAdapter<String>(this, R.layout.lyt_scanlist_textview); 

    Set<BluetoothDevice> bondedSet = bluetoothAdapter.getBondedDevices(); 
       Log.v(BluetoothDemoActivity.LOG, "BluetoothDemo : bondedSet: "+bondedSet); 

       int count = 0; 
       if(bondedSet.size() > 0){ 
        for(BluetoothDevice device : bondedSet){ 
         textViewPairedDevice.setVisibility(View.VISIBLE); 
         bondedAdapter.add(device.getName()+"\n"+device.getAddress()); 
         Log.v(BluetoothDemoActivity.LOG, " count = "+count++); 
        } 
       }else{ 
        bondedAdapter.add("No Devices"); 
       } 

    listViewPairedDevices.setAdapter(bondedAdapter); 
} catch (Exception e) { 
      // TODO Auto-generated catch block 
      Log.e(BluetoothDemoActivity.LOG, e.toString(),e.fillInStackTrace()); 
     } 
    }//onStart ends 
+0

嘿,我知道它很老,但無論如何:P如何連接列表視圖上的設備點擊?你能指點我這樣的事嗎?或編輯.. thx – DavidBalas