2013-05-09 3 views
0

블루투스 장치를 검색하려고합니다. 디버깅 할 때 어댑터가 채워지지만 내 listview wount 새로 고침, 그냥 빈 사이트를보고 있습니다.블루투스 장치 검색 : ListView가 업데이트되지 않습니다.

여기 내 코드입니다 :

package com.example.elevator; 

import java.util.Set; 


import android.os.Bundle; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 


public class MainActivity extends Activity 
{ 
    private static final int REQUEST_ENABLE_BT = 1; 
    private BluetoothAdapter mBluetoothAdapter = null; 
    private ArrayAdapter<String> mNewDevicesArrayAdapter; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_main); 
     ListView newDevicesListView = (ListView) findViewById(R.id.new_devices); 
     newDevicesListView.setAdapter(mNewDevicesArrayAdapter); 
     // newDevicesListView.setOnItemClickListener(mDeviceClickListener); 


     // Register for broadcasts when a device is discovered 
     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     this.registerReceiver(mReceiver, filter); 


     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(mReceiver, filter); 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void onClickbtnActivateBluetooth(View view) 
    { 

      if (mBluetoothAdapter == null) 
      { 
       Toast.makeText(this, "Bluetooth ist nicht verfügbar!", Toast.LENGTH_LONG).show(); 
       finish(); 
       return; 
      } 
      else if (!mBluetoothAdapter.isEnabled()) 
      { 
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
      } 
      Toast.makeText(this, "Bluetooth wurde aktiviert!", Toast.LENGTH_LONG).show(); 
      findViewById(R.id.btnSearchDevices).setEnabled(true); 


    } 

    public void onClickbtnSearchDevices(View view) 
    { 
     mBluetoothAdapter.startDiscovery(); 
     Toast.makeText(this, "Bluetooth Geräte werden gesucht!", Toast.LENGTH_LONG).show(); 
      // Find and set up the ListView for newly discovered devices 

    } 

    // Create a BroadcastReceiver for ACTION_FOUND 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
      @Override 
      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); 
        // If it's already paired, skip it, because it's been listed already 
        if (device.getBondState() != BluetoothDevice.BOND_BONDED) 
        { 
         mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
         mNewDevicesArrayAdapter.notifyDataSetChanged(); 
        } 


       } 
      } 
     }; 

} 

활동 :

<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" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/btnSearchDevices" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/btnEnableBT" 
     android:layout_alignRight="@+id/btnEnableBT" 
     android:layout_below="@+id/btnEnableBT" 
     android:enabled="false" 
     android:onClick="onClickbtnSearchDevices" 
     android:text="Geräte suchen" /> 

    <Button 
     android:id="@+id/btnEnableBT" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:onClick="onClickbtnActivateBluetooth" 
     android:text="Enable Bluetooth" /> 

    <ListView 
     android:id="@+id/new_devices" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/btnSearchDevices" > 

    </ListView> 

</RelativeLayout> 

매니페스트 :

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.elevator" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.SET_DEBUG_APP"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.elevator.MainActivity" 
      android:label="@string/app_name" > 
      android:debuggable="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

나는 당신이 날 도울 수 있기를 바랍니다!

+1

코드는 먼저이 라인'으로 시도 correct.Just입니다 mNewDevicesArrayAdapter = 새로운 ArrayAdapter와 (이, android.R.layout.simple_list_item_1); ' –

답변

0

대신

mNewDevicesArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1); 

당신은이를 시도 할 수 있습니다.

mNewDevicesArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,0); 

목록보기 당신이 사용하여 새 장치를 발견 할 때마다 업데이트됩니다 후 :

mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
1

을 당신은 생성자 인수로 목록을 통과해야합니다.

mNewDevicesArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,devicesList); 
관련 문제