我的应用程序工作正常..但它没有在列表框中显示蓝牙设备...我已经通过应用 toast 来检查它们..所有设备都显示在 toast 但不在列表框上..请帮助..我在这里写代码和布局文件..谢谢
MainActivity.java
package com.example.bluetoothcheck5;
import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected ArrayAdapter<String> mArrayAdapter;
protected BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonSearch = (Button) findViewById(R.id.buttonSearch);
mArrayAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListView listViewPairedDevices = (ListView) findViewById(R.id.listview1);
//BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();
if (mPairedDevices.size() > 0)
{
for (BluetoothDevice mDevice : mPairedDevices)
{
Log.v("Title", "PairedDevices: " + mDevice.getName() + " " + mDevice.getAddress());
Toast.makeText(v.getContext(), mDevice.getName(), Toast.LENGTH_LONG).show();
Toast.makeText(v.getContext(), mDevice.getAddress(), Toast.LENGTH_LONG).show();
listViewPairedDevices.setAdapter(mArrayAdapter);
}
}
}
});}}
activity_main.xml
<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:background="@drawable/ic_launcher"
tools:context=".MainActivity" >
<TextView android:text="" android:id="@+id/out" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TURN_ON" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_toRightOf="@+id/button1"
android:text="DISCOVERABLE" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="TURN_OFF" />
<Button
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:text="Search Devices" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button3" >
</ListView>
</RelativeLayout>