无法将已发现的不成对蓝牙设备添加到Android中的ListView

时间:2014-07-11 13:37:51

标签: java android android-listview bluetooth android-bluetooth

我是Android编程的初学者。我想将配对和不配对的设备添加到ListView,但问题是,只有配对的设备出现在ListView中。

我试图在与蓝牙相关的一些主题的基础上编写代码。这是 MainActivity.java:

package com.example.probax8;

import java.util.Set;

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.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
private final static int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter mBluetoothAdapter = null;
private ArrayAdapter<String> mNewDevicesArrayAdapter;
BroadcastReceiver mReceiver;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //**** Is BlueTooth and enable BT
    final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // Device does not support Bluetooth
    }

    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

    }


    //**** ON/OFF Bluetooth on click

    Button btButton = (Button) findViewById(R.id.bBT);
    btButton.setOnClickListener( new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(!mBluetoothAdapter.isEnabled()){
                    mBluetoothAdapter.enable();
                    }
                    else{
                        mBluetoothAdapter.disable();

                    }
                }
            });
    //**** make Bluetooth discoverable

    Button discButton = (Button) findViewById(R.id.bDis);
    discButton.setOnClickListener( new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent discoverableIntent = new
                            Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                            startActivity(discoverableIntent);
                }
            });




                    tv = (TextView) findViewById(R.id.textView1);
                      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());
                                     tv.setText(device.getName() + "\n" + device.getAddress());

                                 }


                             }
                         }
                     };

                    //****

                    mNewDevicesArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,0);
                    ListView newDevicesListView = (ListView) findViewById(R.id.listView1);
                    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
                    mNewDevicesArrayAdapter.notifyDataSetChanged();
                   // newDevicesListView.setOnItemClickListener(mDeviceClickListener);

                    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                    // If there are paired devices
                    if (pairedDevices.size() > 0) {
                        // Loop through paired devices
                        for (BluetoothDevice device : pairedDevices) {
                            // Add the name and address to an array adapter to show in a ListView
                            mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());

                        }
                    }

                    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                    this.registerReceiver(mReceiver, filter);


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

                     Button unregButton = (Button) findViewById(R.id.bUnreg);
                    unregButton.setOnClickListener( new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    unregisterReceiver(mReceiver);
                                }
                            });

}
}

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"
tools:context="${relativePackage}.${activityClass}" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/bBT"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:text="Bluetooth" />

<Button
    android:id="@+id/bDis"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/bBT"
    android:text="Discoverable for 300 seconds" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/bDis" >
</ListView>

<Button
    android:id="@+id/bRetry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/bBT"
    android:layout_alignBottom="@+id/bBT"
    android:layout_toRightOf="@+id/bBT"
    android:text="Retry" />

<Button
    android:id="@+id/bUnreg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/bRetry"
    android:layout_toRightOf="@+id/bRetry"
    android:text="Unregister" />

</RelativeLayout>

你能帮我解决这个问题吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

最后,我自己想出了答案。 我忘了开始发现:

mBluetoothAdapter.startDiscovery();