2016-04-08 6 views
0

Bluetoothでデバイスを検出しようとしていて、そのBluetoothデバイスをListViewに追加しようとしています。しかし何らかの理由で私のソフトウェアは発見されたデバイスを一つずつリストに追加して、一度に一つのデバイスしかリストに表示されないように常に以前のものを置き換える。ListViewには一度に1つの値しか保持されません

第2の問題は、ソフトウェアがBluetoothデバイスを検出しても、ArrayAdapterが空であるかどうかを確認するifステートメントを実行するということです。

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> 

巨大なありがとう!

答えて

0

新しいリストを毎回作成します。最初の行はonReceive:ArrayList discoverBluetoothDevices = new ArrayList()です。

リストを外に出して追加してください。

arraylistとアダプターの両方をブロードキャストレシーバーの外で宣言して、アダプターのnotifyDataSetChangedを呼び出すだけでリストを更新する必要があります。

+0

これは機能しました。お手伝いありがとう! – jakeheik90

+0

うまくいけば、これを合格としてください。 :) – amosli

+0

もちろんああもちろんです:) – jakeheik90

関連する問題