安卓蓝牙(Bluetooth)之:本地蓝牙操作

本文介绍了一种在Android应用中实现蓝牙功能控制的方法,包括蓝牙的开启与关闭、蓝牙状态的获取等基本操作。

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

蓝牙是一种支持设备间近距离传输数据的无线电技术,支持有蓝牙功能的设备一般有:手机,笔记本电脑,无线蓝牙耳机等。
该小例子就要是获取手机蓝牙基本信息及打开关闭蓝牙操作。
public class MainActivity extends Activity implements View.OnClickListener {
    private static final int OPEN_REQUEST = 11;
    private TextView enable_tv;
    private TextView name_tv;
    private TextView state_tv;
    private Button operator_btn;
    /**
     * BluetoothAdapter代表了本设备(手机、电脑等)的蓝牙适配器对象,通过它可以对蓝牙设备进行如下功能操作:
     * 1、开关蓝牙设备
     * 2、扫描蓝牙设备
     * 3、设置/获取蓝牙状态信息,例如:蓝牙状态值、蓝牙Name、蓝牙Mac地址等
     */
    private BluetoothAdapter mBluetoothAdapter;

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

    /**
     * @description 初始化蓝牙适配器对象
     * @author ldm
     * @time 2017/4/7 16:54
     */
    private void initBluetoothAdapter() {
        //通过getDefaultAdapter获取BluetoothAdapter实例对象
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (null == mBluetoothAdapter) {//当前手机不支持蓝牙功能
            enable_tv.setText("当前手机不支持蓝牙功能");
            name_tv.setText("蓝牙设备:无");
            operator_btn.setVisibility(View.GONE);
        } else {
            enable_tv.setText("当前手机支持蓝牙功能");
            //BluetoothAdapter.getName()方法获取蓝牙名称
            name_tv.setText("蓝牙设备:" + mBluetoothAdapter.getName());
            operator_btn.setVisibility(View.VISIBLE);
        }
        if (mBluetoothAdapter.isEnabled()) {//当前蓝牙处理打开状态
            operator_btn.setText("关闭蓝牙");
        } else {
            operator_btn.setText("打开蓝牙");
        }
        /**
         * 蓝牙状态如:
         * BluetoothAdapter.STATE_ON:蓝牙已经打开
         * BluetoothAdapter.STATE_TURNING_ON:蓝牙正在打开
         * BluetoothAdapter.STATE_TURNING_OFF:蓝牙正在关闭
         * BluetoothAdapter.STATE_OFF:蓝牙已经关闭
         * 其它更多状态
         */
        int state = mBluetoothAdapter.getState();//蓝牙状态

    }


    private void initViews() {
        this.enable_tv = (TextView) findViewById(R.id.enable_tv);
        this.name_tv = (TextView) findViewById(R.id.name_tv);
        this.state_tv = (TextView) findViewById(R.id.state_tv);
        this.operator_btn = (Button) findViewById(R.id.operator_btn);
        this.operator_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.operator_btn) {
            if (!mBluetoothAdapter.isEnabled()) {//如果蓝牙未打开
                //打开蓝牙方式1:调用enable()方法
                mBluetoothAdapter.enable();
                //打开蓝牙方式2:调用系统API打开
//                Intent openIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//                startActivityForResult(openIntent, OPEN_REQUEST);
                operator_btn.setText("关闭蓝牙");
            } else {
                //关闭蓝牙操作
                mBluetoothAdapter.disable();
                operator_btn.setText("打开蓝牙");
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (OPEN_REQUEST == requestCode) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(MainActivity.this, "打开成功", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MainActivity.this, "打开失败", Toast.LENGTH_LONG).show();
            }
        }
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.ldm.bluetoothdemo.MainActivity"
        android:orientation="vertical"
        android:padding="16dp">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android本地蓝牙设备信息"
            android:textSize="16sp"
            android:layout_gravity="center"
            />

    <TextView
            android:id="@+id/enable_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textSize="16sp"
            />

    <TextView
            android:id="@+id/name_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textSize="16sp"
            />

    <TextView
            android:id="@+id/state_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textSize="16sp"
            />

    <Button
            android:id="@+id/operator_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="10dp"
            android:textSize="16sp"
            android:gravity="center"
            />
</LinearLayout>

最后不要忘记添加权限:

<!--配置蓝牙操作权限-->
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
在Android6.0以上系统中,这两个权限也属于普通权限,不用做动态权限处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值