📌  相关文章
📜  如何在Android中以编程方式显示蓝牙配对设备?

📅  最后修改于: 2021-05-09 18:36:32             🧑  作者: Mango

蓝牙技术是一种高速,低功耗的无线技术链路,旨在连接诸如电话或其他便携式设备之类的设备。它具有用于低功率无线电通信的规范(IEEE 802.15.1) ,以无线方式在短距离内链接计算机,电话和其他网络设备。蓝牙信号覆盖的距离通常可达10米或30英尺。蓝牙支持2.45 GHz的波段,并可以在三个语音通道的同时支持高达721 kbps的带宽。该波段已被国际协议搁置,以使用与1.0设备兼容的工业,科学和医疗设备(ISM).rd 。蓝牙能够一次最多连接“八个设备” 。每个设备都提供来自IEEE 802标准的唯一48位地址。蓝牙规范定义并支持各种蓝牙网络连接。以此方式,蓝牙联网可以是用于各种短程应用的无线系统的非常灵活的形式。通过本文,我们希望与您共享一个应用程序的实现,该应用程序显示蓝牙配对设备及其MAC ID的列表。下面给出了一个示例图像,以使您对本文中要做的事情有一个了解。请注意,我们将同时使用JavaKotlin语言来实现此项目。

显示蓝牙配对设备

分步实施

要以编程方式在我们的Android设备上显示蓝牙配对设备的列表,请执行以下步骤:

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言。

步骤2:使用AndroidManifest.xml文件

转到AndroidManifest.xml文件,并添加蓝牙适配器所需的以下权限: BLUETOOTH,BLUETOOTH_ADMINACCESS_COARSE_LOCATION

以下是AndroidManifest.xml文件的完整代码。

XML


    
    
    
    
    
  
    
        
            
                
  
                
            
        
    
  


XML


  
    
    


Kotlin
import android.bluetooth.BluetoothAdapter
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring the textView for name from the layout file
        val tvName = findViewById(R.id.nameTv)
  
        // Declaring the textView for MAC ID from the layout file
        val tvMac = findViewById(R.id.macAddressTv)
  
        // Declaring the button from the layout file
        val btn = findViewById


Java
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Set;
  
public class MainActivity extends AppCompatActivity {
  
    TextView tvName, tvMac;
    Button btn;
    BluetoothAdapter bAdapter;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Declaring the textView for name from the layout file
        tvName = (TextView) findViewById(R.id.nameTv);
  
        // Declaring the textView for MAC ID from the layout file
        tvMac = (TextView) findViewById(R.id.macAddressTv);
  
        // Declaring the button from the layout file
        btn = (Button) findViewById(R.id.btnGet);
  
        // Initializing the Bluetooth Adapter
        bAdapter = BluetoothAdapter.getDefaultAdapter();
  
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Checks if Bluetooth Adapter is present
                if (bAdapter == null) {
                    Toast.makeText(getApplicationContext(), "Bluetooth Not Supported", Toast.LENGTH_SHORT).show();
                } else {
                    // List all the bonded devices(paired)
                    Set pairedDevices = bAdapter.getBondedDevices();
                    if (pairedDevices.size() > 0) {
                        for (BluetoothDevice device : pairedDevices) {
  
                            // get the device name
                            String deviceName = device.getName();
  
                            // get the mac address
                            String macAddress = device.getAddress();
  
                            // append in the two separate views
                            tvName.append(deviceName + "\n");
                            tvMac.append(macAddress + "\n");
                        }
                    }
                }
            }
        });
    }
}


步骤3:使用activity_main.xml文件

现在转到代表应用程序UI的activity_main.xml文件。创建一个布局,该布局将显示蓝牙设备的配对列表及其MAC地址,以及一个用于获取它们的按钮。以下是activity_main.xml文件的代码。在代码内部添加了注释,以更详细地了解代码。

XML格式



  
    
    

步骤4:使用MainActivity文件

转到MainActivity文件,并参考以下代码。下面是MainActivity文件的代码。在代码内部添加了注释,以更详细地了解代码。

科特林

import android.bluetooth.BluetoothAdapter
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring the textView for name from the layout file
        val tvName = findViewById(R.id.nameTv)
  
        // Declaring the textView for MAC ID from the layout file
        val tvMac = findViewById(R.id.macAddressTv)
  
        // Declaring the button from the layout file
        val btn = findViewById

Java

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Set;
  
public class MainActivity extends AppCompatActivity {
  
    TextView tvName, tvMac;
    Button btn;
    BluetoothAdapter bAdapter;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Declaring the textView for name from the layout file
        tvName = (TextView) findViewById(R.id.nameTv);
  
        // Declaring the textView for MAC ID from the layout file
        tvMac = (TextView) findViewById(R.id.macAddressTv);
  
        // Declaring the button from the layout file
        btn = (Button) findViewById(R.id.btnGet);
  
        // Initializing the Bluetooth Adapter
        bAdapter = BluetoothAdapter.getDefaultAdapter();
  
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Checks if Bluetooth Adapter is present
                if (bAdapter == null) {
                    Toast.makeText(getApplicationContext(), "Bluetooth Not Supported", Toast.LENGTH_SHORT).show();
                } else {
                    // List all the bonded devices(paired)
                    Set pairedDevices = bAdapter.getBondedDevices();
                    if (pairedDevices.size() > 0) {
                        for (BluetoothDevice device : pairedDevices) {
  
                            // get the device name
                            String deviceName = device.getName();
  
                            // get the mac address
                            String macAddress = device.getAddress();
  
                            // append in the two separate views
                            tvName.append(deviceName + "\n");
                            tvMac.append(macAddress + "\n");
                        }
                    }
                }
            }
        });
    }
}

输出:在物理设备上运行

显示蓝牙配对设备

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!