可用的蓝牙设备列表

时间:2019-05-08 20:29:22

标签: java android

首先,我是android编程的新手 现在我的问题是我想创建一个列出附近可用蓝牙设备的应用程序,我阅读了android概述,并尝试遵循它,但是我的代码不起作用,可能是问题所在

我已经阅读了一些文章,但无法正常工作

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));
                    }
                }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

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));