Я пытаюсь обнаружить устройства с Bluetooth и добавить обнаруженные Bluetooth-устройства в ListView. Но по какой-то причине мое программное обеспечение добавляет эти обнаруженные устройства в список один за другим и всегда заменяет предыдущий, так что в списке одновременно отображается только одно устройство.ListView имеет только одно значение за один раз
Вторая проблема заключается в том, что, хотя мое программное обеспечение находит устройства Bluetooth, оно выполняет оператор if, где он проверяет, является ли ArrayAdapter пустым.
private final BroadcastReceiver Receiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent){
ArrayList discoveredBluetoothDevices = new ArrayList();
final ArrayAdapter discoveredBluetoothAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, discoveredBluetoothDevices);
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, check first
// it isn't already paired device
if(device.getBondState() != BluetoothDevice.BOND_BONDED) {
discoveredBluetoothDevices.add(device.getName() + "\n" + device.getAddress());
DiscoveredBluetoothDeviceList.setAdapter(discoveredBluetoothAdapter);
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//Set the activity title
setTitle(R.id.bluetooth);
if(discoveredBluetoothAdapter.getCount() == 0){
Toast.makeText(getApplicationContext(), "No devices found", Toast.LENGTH_SHORT).show();
}
}
}
};
ListView DiscoveredBluetoothDeviceList = (ListView) findViewById(R.id.BluetoothDeviceList);
// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(Receiver, filter);
// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(Receiver, filter);
И мой XML-файл:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bluetooth"
tools:context="com.example.jake.bluetooth.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/BluetoothDeviceList"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="#000000"
android:dividerHeight="1dp" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SetOnBluetooth"
android:id="@+id/SetOnBluetoothButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="133dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SetOffBluetooth"
android:id="@+id/SetOffBluetoothButton"
android:layout_above="@+id/SetOnBluetoothButton"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Query"
android:id="@+id/QueryButton"
android:layout_above="@+id/ReceiveButton"
android:layout_alignLeft="@+id/ReceiveButton"
android:layout_alignStart="@+id/ReceiveButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DiscoverDevices"
android:id="@+id/DiscoverDevicesButton"
android:layout_centerHorizontal="true"
android:layout_above="@+id/SetOffBluetoothButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Connect"
android:id="@+id/ConnectButton"
android:layout_alignTop="@+id/SetOnBluetoothButton"
android:layout_toLeftOf="@id/SetOnBluetoothButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Send"
android:id="@+id/SendButton"
android:layout_toLeftOf="@+id/SetOffBluetoothButton"
android:layout_alignTop="@+id/SetOffBluetoothButton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Receive"
android:id="@+id/ReceiveButton"
android:layout_alignTop="@+id/SetOnBluetoothButton"
android:layout_toRightOf="@+id/SetOnBluetoothButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Visible"
android:id="@+id/VisibleButton"
android:layout_above="@+id/DiscoverDevicesButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Exit"
android:id="@+id/ExitButton"
android:layout_above="@+id/VisibleButton"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Огромное спасибо заранее, если вы в состоянии помочь мне!
Это сработало. Спасибо вам за помощь! – jakeheik90
Можете ли вы установить это как принятое, если оно сработает? :) – amosli
О да конечно :) – jakeheik90