android_mars老师_蓝牙学习1

该文章展示了如何在Android应用中检查设备是否具有蓝牙功能,并进行蓝牙设备扫描。首先在AndroidManifest.xml中配置蓝牙权限,然后在MainActivity中获取BluetoothAdapter对象,判断蓝牙是否开启,如果开启则扫描已配对的蓝牙设备并打印其地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现功能:

查看本机是否有蓝牙功能、扫瞄周边蓝牙获取其地址。

效果展示:

效果展示

具体流程:

  1. AndroidManifest.xml配置蓝牙权限
  2. activity_main.xml绘制页面_按钮
  3. MainActivity实现:点击事件监听按钮------>创建一个bluetoothAdapter对象------->bluetoothAdapter对象为空则本机不存在蓝牙功能-------->如存在,bluetoothAdapter.isEnabled()查看蓝牙功能是否被开启-------->bluetoothAdapter.getBondedDevices()扫描蓝牙。

代码展示

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.bluetooth">

    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BlueTooth"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scanBlurtooth"
        android:text="扫描蓝牙设备"
        app:layout_constraintTop_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

package com.example.bluetooth;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Iterator;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private Button bluetoothButton = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bluetoothButton = (Button) findViewById(R.id.scanBlurtooth);
        bluetoothButton.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("MissingPermission")
            @Override
            public void onClick(View v) {
                //gain a BluetoothAdapter Object.
                BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                //judge whether current device has bluetooth work.
                //if bluetoothAdapter == null,it means current device don't have bluetooth work.
                if(bluetoothAdapter != null) {
                    System.out.println("device has bluetooth function");
                    //judge whether bluetooth is opened.
                    if (bluetoothAdapter.isEnabled()) {
                        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivity(intent);
                    }
                    //obtain all connected devices
                    Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
                    if(devices.size()>0){
                        for(Iterator iterator = devices.iterator();iterator.hasNext();){
                            BluetoothDevice bluetoothDevice =(BluetoothDevice) iterator.next();
                            System.out.println(bluetoothDevice.getAddress());
                        }
                    }
                }else {
                    System.out.println("device can't use bluetooth");
                }
            }
        });
    };
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值