Bluetooth'lu aygıtları keşfetmeye çalışıyorum ve bu keşfedilen Bluetooth aygıtlarını bir ListView'e ekliyorum. Fakat bir nedenden dolayı, yazılımım bu keşfedilen cihazları teker teker listeye ekler ve her zaman bir öncekini değiştirir, böylelikle listede bir defada sadece bir cihaz gösterilir.ListView, bir seferde yalnızca bir değeri tutar
İkinci sorun, yazılımım Bluetooth aygıtlarını bulsa da, ArrayAdapter öğesinin boş olup olmadığını kontrol ettiği if deyimini yürütmesidir.
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);
Ve xml dosyası: Eğer bana yardım edebiliyoruz önceden
<?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>
Huge teşekkürler!
Bu çalıştı. Yardım için teşekkürler! – jakeheik90
Çalıştıysa bunu kabul edilebilir olarak ayarlayabilir misiniz? :) – amosli
Oh evet tabiki :) – jakeheik90