Android 正在发现蓝牙设备:ListView将不会更新
我正在尝试发现蓝牙设备。 当我调试时,我的适配器被填满了,但是我的listview没有刷新,我看到的只是一个空白的站点 这是我的密码:Android 正在发现蓝牙设备:ListView将不会更新,android,listview,bluetooth,adapter,Android,Listview,Bluetooth,Adapter,我正在尝试发现蓝牙设备。 当我调试时,我的适配器被填满了,但是我的listview没有刷新,我看到的只是一个空白的站点 这是我的密码: package com.example.elevator; import java.util.Set; import android.os.Bundle; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.
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();
}
}
}
};
}
package com.example.lifter;
导入java.util.Set;
导入android.os.Bundle;
导入android.app.Activity;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.view.Menu;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
公共类MainActivity扩展了活动
{
私有静态最终整数请求_ENABLE_BT=1;
私有BluetoothAdapter mBluetoothAdapter=null;
专用阵列适配器mNewDevicesArrayAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNewDevicesArrayAdapter=新的ArrayAdapter(这个,R.layout.activity_main);
ListView新设备ListView=(ListView)findViewById(R.id.new_设备);
setAdapter(mNewDevicesArrayAdapter);
//setOnItemClickListener(mDeviceClickListener);
//在发现设备时注册广播
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
此。注册表接收程序(mReceiver,过滤器);
过滤器=新的IntentFilter(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED);
此。注册表接收程序(mReceiver,过滤器);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
}
@凌驾
公共布尔onCreateOptions菜单(菜单)
{
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
单击bTnActivateBluetooth(视图)时出现公共无效
{
if(mBluetoothAdapter==null)
{
Toast.makeText(这个“蓝牙是nicht verfügbar!”,Toast.LENGTH_LONG.show();
完成();
返回;
}
如果(!mBluetoothAdapter.isEnabled()),则为else
{
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
Toast.makeText(这是“Bluetooth wurde aktiviert!”,Toast.LENGTH_LONG.show();
findViewById(R.id.btnSearchDevices).setEnabled(true);
}
公共void onClickbtnSearchDevices(视图)
{
mBluetoothAdapter.startDiscovery();
Toast.makeText(这是“Bluetooth Geräte werden gesucht!”,Toast.LENGTH_LONG.show();
//查找并设置新发现设备的ListView
}
//为找到的操作创建广播接收器
专用最终广播接收器mReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
//当发现发现发现设备时
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
//从Intent获取BluetoothDevice对象
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
//如果它已配对,请跳过它,因为它已列出
if(device.getBondState()!=BluetoothDevice.BOND\u BOND)
{
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>
android:debuggable=“true”>
我希望你能帮助我 而不是
mNewDevicesArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
必须将列表作为构造函数参数传递
mNewDevicesArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,devicesList);
mNewDevicesArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,DeviceList);
您的代码是正确的。只需先尝试这一行mNewDevicesArrayAdapter=new ArrayAdapter(这是android.R.layout.simple_list_item_1)代码>
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
mNewDevicesArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,devicesList);