如何获取附近所有可发现蓝牙设备的列表?

时间:2017-08-03 07:50:05

标签: android bluetooth

我想在我的应用程序中获取附近所有可发现蓝牙设备的列表,但我没有得到任何列表。我的deviceDiscovery()方法执行正常,但在onCreate()的BroadcastReceiver中的onReceive()中没有得到任何响应。谁能告诉我为什么这样?这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    BluetoothAdapter ba;
    ToggleButton tb;
    ArrayAdapter aa;
    ListView l;
    private static final int ENABLE_REQUEST=1;
    private static final int DISCOVERABLE_REQUEST=2;
    private static final int DISCOVERABLE_DURATION=10;


    BroadcastReceiver br=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {


            String action=intent.getAction();

            if(BluetoothDevice.ACTION_FOUND.equals(action))
            {
              BluetoothDevice bd=  intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                aa.add(bd.getName()+"  "+bd.getAddress());


            }
        }
    };






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tb= (ToggleButton) findViewById(R.id.toggle);
aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1);
        l= (ListView) findViewById(R.id.list);
        l.setAdapter(aa);


        ba= BluetoothAdapter.getDefaultAdapter();

        tb.setOnClickListener(this);
    }




    @Override
    public void onClick(View v) {

        aa.clear();

        ToggleButton tb= (ToggleButton) v;

        if(ba==null) {
            Toast.makeText(this, "This feature is not supported", Toast.LENGTH_SHORT).show();
            tb.setChecked(false);
        }
        else
        {
            if(tb.isChecked())
            {
                if(!ba.isEnabled())
                {
                    Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(i,ENABLE_REQUEST);
                }
                else
                {
                    Toast.makeText(this, "Your device is already enabled", Toast.LENGTH_SHORT).show();
                    deviceDiscovery();
                    makeDiscoverable();
                }
            }
            else
            {
                aa.clear();
                ba.disable();
                Toast.makeText(this, "Your device is now disabled", Toast.LENGTH_SHORT).show();
            }
        }



    }








    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch(requestCode)
        {
            case ENABLE_REQUEST:if(resultCode==RESULT_OK)
            {
                Toast.makeText(this, "Bluetooth Enabled", Toast.LENGTH_SHORT).show();
                deviceDiscovery();
                makeDiscoverable();
            }
            else
            {
                Toast.makeText(this, "Bluetooth Enabling Rejected", Toast.LENGTH_SHORT).show();
                tb.setChecked(false);
            }
                break;

            case DISCOVERABLE_REQUEST:if(resultCode==DISCOVERABLE_DURATION)
            {
                Toast.makeText(this, "Your device is discoverable by other devices", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(this, "Your device is not discoverable", Toast.LENGTH_SHORT).show();
            }
                break;
        }
    }


    private void makeDiscoverable() {
        Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,DISCOVERABLE_DURATION);
        startActivityForResult(i,DISCOVERABLE_REQUEST);
    }



    private void deviceDiscovery()
    {
if(ba.startDiscovery())
{
    IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);

    this.registerReceiver(br,filter);
    Toast.makeText(this, "Discovering other bluetooth devices", Toast.LENGTH_SHORT).show();
}
else
{
    Toast.makeText(this, "Discovery failed to start", Toast.LENGTH_SHORT).show();
}

    }


}

MainActivity.xml

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="On"
        android:textOff="Off"
        android:id="@+id/toggle"
        />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_below="@+id/toggle"/>

的AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

1 个答案:

答案 0 :(得分:1)

添加filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);并运行项目。您可以阅读更多Getting the address and names of all available bluetooth devices in android