简体   繁体   中英

List of Available Bluetooth Devices

First of all I am new to android programming now my problem is I want to create an app which lists the available nearby Bluetooth devices, I read the android Overview and I tried to follow it, but my code doesn't work, what can be the problem

I have already read some articles but I couldn't get it to work

package com.example.arduinobluetoothinterface;
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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

public class Main2Activity extends AppCompatActivity {
    //set an ArrayList for the bluetooth Devices Names
    ArrayList<BtDevicesClass> listOfDevices = new ArrayList<>();
    final BluetoothAdapter BluetoothDevices = BluetoothAdapter.getDefaultAdapter();
    // Create a BroadcastReceiver for ACTION_FOUND.
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Discovery has found a device. Get the BluetoothDevice
                // object and its info from the Intent.
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                listOfDevices.add(new BtDevicesClass(deviceHardwareAddress,deviceName));
            }
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Don't forget to unregister the ACTION_FOUND receiver.
        unregisterReceiver(receiver);
        BluetoothDevices.disable();

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(receiver, filter);
        setContentView(R.layout.activity_main2);
        //set the bluetooth adapter
        //set a List view for the bluetooth Devices
        ListView listofItems = (ListView) findViewById(R.id.root_view);
        //set Adapter
        ArrayAdapterBt itemAdapter = new ArrayAdapterBt(this, listOfDevices);
        //append list item to ArrayAdapter
        listofItems.setAdapter(itemAdapter);
        //check if the bluetooth service is avaliable on the device
        if (BluetoothDevices != null) { //check if the device supports bluetooth
            /*
             ** if the bluetooth module is not enabled this block of code pops-up a message to
             * enable it via an Intent , this is done by performing an Intent as shown below
             */
           if (!BluetoothDevices.isEnabled()) {
                Intent bluetoothenableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(bluetoothenableIntent, 0);
            }
                Toast.makeText(getApplicationContext(),"Searching for nearby Devices",Toast.LENGTH_SHORT).show();
                Intent makeDeviceDiscouvrable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                startActivityForResult(makeDeviceDiscouvrable,0);
                BluetoothDevices.startDiscovery();
                Set<BluetoothDevice> pairedDevices = BluetoothDevices.getBondedDevices();
                if (pairedDevices.size() > 0) {
                    // There are paired devices. Get the name and address of each paired device.
                    for (BluetoothDevice device : pairedDevices) {
                        String deviceName = device.getName();
                        String deviceHardwareAddress = device.getAddress(); // MAC address
                        listOfDevices.add(new BtDevicesClass(deviceHardwareAddress,deviceName));
                    }
                }

        }
    }
}

Try this code:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

List<String> bluetoothList = new ArrayList<String>();
for(BluetoothDevice bluetooth : pairedDevices)
    bluetoothList.add(bluetooth.getName());

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, bluetoothList));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM