Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理

27 篇文章 3 订阅

 

 

Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理

 

目录

Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理

一、简单介绍

二、实现原理

三、注意事项

四、效果预览

五、实现步骤

六、关键代码


 

一、简单介绍

Android 开发中的一些基础操作,使用整理,便于后期使用。

本节介绍,Android 开发中,获取手机设备的蓝牙Bluetooth的状态,并设置蓝牙Bluetooth开关,监听蓝牙Bluetooth的状态变化,方法不唯一,欢迎指正。

 

二、实现原理

1、获取蓝牙适配器 BluetoothAdapter.getDefaultAdapter()

2、bluetoothAdapter.isEnabled() 获取蓝牙的状态

3、bluetoothAdapter.enable()/bluetoothAdapter.disable() 设置蓝牙开关状态

4、使用广播监听 BluetoothAdapter.ACTION_STATE_CHANGED 蓝牙状态变化

 

三、注意事项

1、添加蓝牙权限

<uses-permission android:name="android.permission.BLUETOOTH"/>

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

 

四、效果预览

 

五、实现步骤

1、打开 Android Studio 新建一个工程,或者新建一个模块

 

2、默认操作,一路创建一个模块

 

3、创建一个 BluetoothUtil,编写获取蓝牙状态,设置蓝牙状态,和监听蓝牙状态变化的方法

 

4、MainActivity ,测试 BluetoothUtil 封装的方法

 

5、打包,在设备上测试,效果如上

 

六、关键代码

1、BluetoothUtil

package com.example.bluetoothtest;

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.util.Log;
import android.widget.Toast;

public class BluetoothUtil {

    private static final String TAG = "Main";

    private BluetoothAdapter bluetoothAdapter;
    private BluetoothStateBroadcastReceive mReceive;

    public BluetoothUtil() {
        // 获得蓝牙适配器对象
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public boolean getBlueToothState() {
        // 获取蓝牙状态
        return bluetoothAdapter.isEnabled();
    }

    public boolean openBlueTooth() {
        if (getBlueToothState()) return true;
        // 打开蓝牙
        return bluetoothAdapter.enable();
    }

    public boolean colseBlueTooth() {
        if (!getBlueToothState()) return true;
        // 关闭蓝牙
        return bluetoothAdapter.disable();
    }

    // 调用系统的请求打开蓝牙
    public void gotoSystem(Context context){
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        context.startActivity(intent);
    }

    public void registerBluetoothReceiver(Context context){
        if(mReceive == null){
            mReceive = new BluetoothStateBroadcastReceive();
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_OFF");
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_ON");
        context.registerReceiver(mReceive, intentFilter);
    }

    public void unregisterBluetoothReceiver(Context context){
        if(mReceive != null){
            context.unregisterReceiver(mReceive);
            mReceive = null;
        }
    }

    class BluetoothStateBroadcastReceive extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            switch (action){
                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    Toast.makeText(context , "蓝牙设备:" + device.getName() + "已连接", Toast.LENGTH_SHORT).show();
                    Log.i(TAG, "onReceive: "+"蓝牙设备:" + device.getName() + "已连接");
                    break;
                case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    Toast.makeText(context , "蓝牙设备:" + device.getName() + "已断开", Toast.LENGTH_SHORT).show();
                    Log.i(TAG, "onReceive: "+"蓝牙设备:" + device.getName() + "已断开");
                    break;
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    switch (blueState){
                        case BluetoothAdapter.STATE_OFF:
                            Toast.makeText(context , "蓝牙已关闭", Toast.LENGTH_SHORT).show();
                            Log.i(TAG, "onReceive: "+"蓝牙已关闭:" );
                            break;
                        case BluetoothAdapter.STATE_ON:
                            Toast.makeText(context , "蓝牙已开启"  , Toast.LENGTH_SHORT).show();
                            Log.i(TAG, "onReceive: "+"蓝牙已开启:");
                            break;
                    }
                    break;
            }
        }
    }
}

 

2、MainActivity

package com.example.bluetoothtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    private  static  final  String TAG = "MainActivity";

    BluetoothUtil _BluetoothUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        _BluetoothUtil = new BluetoothUtil();
        _BluetoothUtil.registerBluetoothReceiver(this);
        Log.i(TAG, "onCreate: BT State : "+ _BluetoothUtil.getBlueToothState());
        _BluetoothUtil.colseBlueTooth();
        Log.i(TAG, "onCreate: BT State : "+ _BluetoothUtil.getBlueToothState());
        _BluetoothUtil.openBlueTooth();
        //_BluetoothUtil.gotoSystem(this);
        Log.i(TAG, "onCreate: BT State : "+ _BluetoothUtil.getBlueToothState());
    }

    @Override
    protected void onDestroy() {

        _BluetoothUtil.unregisterBluetoothReceiver(this);
        super.onDestroy();
    }
}

 

3、AndroidManifest.xml

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

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

 

 

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
使用Android Studio开发一个蓝牙BLE传输的App相对简单,以下是一个基本的步骤: 1. 首先,在Android Studio中创建一个新的项目。选择Empty Activity模板,并在后续配置中选择使用Kotlin或Java作为开发语言。 2. 在AndroidManifest.xml文件中添加蓝牙相关的权限和特性声明,例如: <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 3. 在build.gradle(Module:app)文件中添加Bluetooth API的依赖项,例如: implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:cardview-v7:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' 4. 创建一个新的Activity用于扫描和连接蓝牙设备,例如BluetoothActivity。 5. 在BluetoothActivity的布局文件中添加一个RecyclerView用于显示扫描到的蓝牙设备列表,并在代码中初始化RecyclerView的适配器。 6. 在BluetoothActivity的代码中,使用BluetoothAdapter类扫描周围的蓝牙设备,并将它们添加到RecyclerView的适配器中。 7. 在RecyclerView的适配器中,为每个蓝牙设备创建一个点击事件监听器,以便用户可以选择一个设备来建立BLE连接。 8. 创建一个新的Activity用于处理已连接的蓝牙设备,并在该Activity的布局文件中添加一个读写蓝牙数据的按钮。 9. 在该Activity的代码中,使用BluetoothGatt类来连接已选中的设备,并实现相应的监听器来处理BLE连接状态和数据传输。 10. 使用BluetoothGatt类的方法来读取、写入和监听蓝牙数据的变化。 11. 最后,在Manifest文件中为每个Activity添加相应的声明,以便在运行时访问蓝牙权限。 通过以上步骤,你就可以使用Android Studio开发一个简单的蓝牙BLE传输的App。当然,具体的代码实现细节还需要根据你的需求和具体的蓝牙设备进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仙魁XAN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值