2011-08-26 210 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