android - Discovering Bluetooth Devices: ListView will not get updated -
i trying discover bluetooth devices. when i'm debugging adapter gets filled, listview wount refresh, seeing blank site.
here 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 broadcasts when device 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 menu; adds items action bar if 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 , set listview newly discovered devices } // create broadcastreceiver action_found private final broadcastreceiver mreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { string action = intent.getaction(); // when discovery finds device if (bluetoothdevice.action_found.equals(action)) { // bluetoothdevice object intent bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device); // if it's paired, skip it, because it's been listed if (device.getbondstate() != bluetoothdevice.bond_bonded) { mnewdevicesarrayadapter.add(device.getname() + "\n" + device.getaddress()); mnewdevicesarrayadapter.notifydatasetchanged(); } } } }; }
the activity:
<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>
the manifest:
<?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 can me!
you have pass list constructor argument.
mnewdevicesarrayadapter=new arrayadapter<string>(this,android.r.layout.simple_list_item_1,deviceslist);
Comments
Post a Comment