发现蓝牙设备:ListView控件将不会被更新 [英] Discovering Bluetooth Devices: ListView will not get updated

查看:113
本文介绍了发现蓝牙设备:ListView控件将不会被更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发现蓝牙设备。 当我调试我的适配器得到填补,但我的列表视图wount刷新,我看到只是一个空白点。

I am trying to discover Bluetooth Devices. When I'm debugging my adapter gets filled, but my listview wount refresh, I am seeing just a blank site.

这是我的code:

    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>

我希望你能帮帮我!

I hope you can help me!

推荐答案

您必须将名单通过构造函数参数。

You have to pass the list as a constructor argument.

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

这篇关于发现蓝牙设备:ListView控件将不会被更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆