【问题标题】:Get Access to BluetoothDevices from Set从 Set 访问蓝牙设备
【发布时间】:2012-09-03 17:27:25
【问题描述】:

我正在尝试在 Android 和蓝牙模块之间建立连接。我按照http://developer.android.com/guide/topics/connectivity/bluetooth.html 的指示进行操作 在“发现”之后,我将找到的设备放入一个 ListView 中,该 ListView 有一个注册的OnItemClickListener。

我现在的问题是我需要单击条目中的BluetoothDevice 来在下一步中建立连接。我所拥有的只是 ListView 中的一个位置 ID 和一个 Set<BluetoothDevice>。但我不知道如何从集合中提取特定的BluetoothDevice。

到目前为止,这是我的代码。 谢谢!!

ProgressBar spinWheel;
ListView devList;
BluetoothAdapter btAdapter;
ArrayAdapter<String> lvAdapter;
BroadcastReceiver mReceiver;
Set<BluetoothDevice> pairedDevices;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searchlist);
    spinWheel = (ProgressBar)findViewById(R.id.progressBar1);
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    devList = (ListView)findViewById(R.id.devicelist);
    lvAdapter = new ArrayAdapter<String>(getApplicationContext(),
            R.layout.simplerow, R.id.simpleRow);
    devList.setAdapter(lvAdapter);
    devList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parentView, View childView, int position,
                long id) {
            //establish Connection, need the clicked BluetoothDevice
        }
    });

    //gepaarte Geräte in die ListView
    pairedDevices = btAdapter.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
            lvAdapter.add(device.getName() + "\n"+ "#"+ device.getAddress());
        }
    }

    // Create a BroadcastReceiver for ACTION_FOUND
        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);
                // Add the name and address to an array adapter to show in a ListView
                lvAdapter.add(device.getName() + "\n" + device.getAddress());
                pairedDevices.add(device);
            }
        }
    };
    // Register the BroadcastReceiver
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
    btAdapter.startDiscovery();
    }

@Override
public void onDestroy(){
    super.onDestroy();
    unregisterReceiver(mReceiver);
}

}

【问题讨论】:

    标签: listview bluetooth set onitemclick


    【解决方案1】:

    我自己解决了这个问题,但我想发布它以帮助其他人。 我使用了函数 BluetoothAdapter.getRemoteDevice(String adress)。 作为从ListView中插入String的地址,在“#”处拆分。

    public void onItemClick(AdapterView<?> parentView, View childView, int position,
                    long id) {
                String[] separated = lvAdapter.getItem(position).split("#");
                if(btAdapter.checkBluetoothAddress(separated[1])==true){
                devtoconnect = btAdapter.getRemoteDevice(separated[1]);
                }
            }
        });
    

    仍然感谢您在其他问题上的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      相关资源
      最近更新 更多