当前位置: 移动技术网 > 移动技术>移动开发>Android > Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理

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

2020年08月01日  | 移动技术网移动技术  | 我要评论
Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理目录Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理一、简单介绍二、实现原理三、注意事项四、效果预览五、实现步骤六、关键代码一、简单介绍Android 开发中的...

 

 

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>

 

 

本文地址:https://blog.csdn.net/u014361280/article/details/108150955

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网