There is a need to send the names of all found bluetooth devices to the server.

The data in the listadapter and listview , how to send the device names to the server in the format " BT_name 1, BT_name 2 ... etc." Code:

 public class SearchActivity extends ListActivity { private BluetoothAdapter bluetoothAdapter; private BroadcastReceiver discoverDevicesReceiver; private BroadcastReceiver discoveryFinishedReceiver; private final List<BluetoothDevice> discoveredDevices = new ArrayList<BluetoothDevice>(); private ArrayAdapter<BluetoothDevice> listAdapter; private ProgressDialog progressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); listAdapter = new ArrayAdapter<BluetoothDevice>(getBaseContext(), android.R.layout.simple_list_item_1, discoveredDevices) { @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); final BluetoothDevice device = getItem(position); ((TextView) view.findViewById(android.R.id.text1)).setText(device.getName()); return view; } }; setListAdapter(listAdapter); } public void discoverDevices(View view) { discoveredDevices.clear(); listAdapter.notifyDataSetChanged(); if (discoverDevicesReceiver == null) { discoverDevicesReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (!discoveredDevices.contains(device)) { discoveredDevices.add(device); listAdapter.notifyDataSetChanged(); } } } }; } if (discoveryFinishedReceiver == null) { discoveryFinishedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { getListView().setEnabled(true); if (progressDialog != null) progressDialog.dismiss(); unregisterReceiver(discoveryFinishedReceiver); } }; } registerReceiver(discoverDevicesReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); registerReceiver(discoveryFinishedReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)); getListView().setEnabled(false); progressDialog = ProgressDialog.show(this, "Поиск устройств", "Подождите..."); bluetoothAdapter.startDiscovery(); } @Override public void onPause() { super.onPause(); bluetoothAdapter.cancelDiscovery(); if (discoverDevicesReceiver != null) { try { unregisterReceiver(discoverDevicesReceiver); } catch (Exception e) { Log.d("SearchActivity", "Error turn off reciever: " + discoverDevicesReceiver); } } } } 

I need to send them all at once, not one by one. How do I send, I know, I need to form the format of the above template from the data in the listadapter 'e.

  • 2
    Form a format? - Nick Volynkin
  • four
    in the wording "take data from the listview" you will not go far. Because there is no data in the listview. It only displays what the adapter gives it. Therefore, you need to look at where the adapter takes the data and form it for example json (or xml. Or, in extreme cases, a plain text file) - KoVadim
  • @KoVadim Yes, I was wrong, I indicated in the subject line that the data in the adapter. Data adapter takes from bluetooth.startDiscovery. - Ilya Nenilin
  • @NickVolynkin Yes, form the data from the adapter according to a template so that one request to send - Ilya Nenilin
  • @NicolasChabanovsky updated the question, added the code. How can I send to the server the names of all found devices at once? - Ilya Nenilin

1 answer 1

BluetoothDevice has getName()

What else do you need?

 StringBuilder deviceNames = new StringBuilder; for(BluetoothDevice device: discoveredDevices) deviceNames.append(device.getName()+",");