Android蓝牙详析 | 蓝牙的适配、权限、开启、搜索发现等处理

本系列笔记概述

  • 蓝牙传输优势:功耗低,传输距离还可以;

  • 蓝牙聊天室案例

  • Android中蓝牙设备的使用
    • 蓝牙权限(本文的讲解内容之一)
    • 蓝牙功能开启(本文的讲解内容之一)
    • 搜索蓝牙设备(本文的讲解内容之一)
    • 与外设搭建RFCOMM通道(射频通道)
    • 蓝牙设备双向数据传输

蓝牙聊天室案例框架

  • 蓝牙权限
    • 执行蓝牙通信需要权限BLUETOOTH,
      例如:请求连接、接收连接和传输数据等;

    • 如果需要启动设备 或 操作蓝牙设置,则需声明BLUETOOTH_ADMIN权限

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  • 设置蓝牙——获取BlueAdapter
    • 使用蓝牙需用到BlueAdapter。表示设备自身的蓝牙适配器;

    • 通过静态方法BlueAdapter.getDefaultAdapter()获得BlueAdapter;

    • 整个系统只有一个蓝牙适配器,application可使用此BlueAdapter对象与之交互;

    • 如果getDefaultAdapter()返回null,则表示该设备不支持蓝牙,
      例如:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(mBluetoothAdapter == null){
            //Device does not support Bluetooth
        }
  • 启用蓝牙
    • 调用isEnable()以检查当前是否已启用蓝牙;
      如果此方法返回false,则表示蓝牙处于停用状态;

    • 要请求启用蓝牙,将通过ACTION_REQUEST_ENABLE向系统设置
      发出启用蓝牙的请求(无需停止应用),
      例如:

...
    private static final int REQUEST_ENABLE_BT = 10;//其是需要自己定义的局部常量。
...

        if(mBluetoothAdapter.isEnabled()){
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

demo(查看本机是否支持蓝牙,蓝牙是否开启,没开启则请求):

  • 新建一个项目,添加好如上述两个权限,编写MainActivity.java:
public class MainActivity extends AppCompatActivity {


    private static final String TAG = "BluetoothChat";
    private static final int REQUEST_ENABLE_BT = 10;//其是需要自己定义的局部常量。
    private BluetoothAdapter mBluetoothAdapter;

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

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(mBluetoothAdapter == null){
            //Device does not support Bluetooth
            Log.e(TAG, "Device does not support Bluetooth");
        }else {
            Toast.makeText(this,"设备支持蓝牙!",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        if(!mBluetoothAdapter.isEnabled()){
            //向系统请求开启蓝牙
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);//结果返回回调到onActivityResult()
        }else {
            //已经开启了蓝牙
            Toast.makeText(this,"蓝牙已经开启",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_ENABLE_BT){
            Toast.makeText(this,"蓝牙已经开启",Toast.LENGTH_SHORT).show();
        }
    }
}


  • 查找设备——查找配对过的设备
    • getBondedDevices():返回已配对设备的一组BluetoothDevice
 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                //If threre are paired devices
                if (pairedDevices.size() > 0) {
                    //Loop through paired devices
                    for(BluetoothDevice device : pairedDevices){
                        //Add the name and address to an array adapter to show in a ListView
                        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                    }
                }
  • 查找设备——发现设备
    • 发现设备:startDiscovery()
      该进程为异步进程,
      该方法会立即返回一个布尔值,指示是否已成功启动发现操作
    • 发现进程通常包含约12秒钟查询扫描
  1. 广播接收:
 //广播接收
    private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();//获取action
            Log.d(TAG, "ACTION:" + action);
            if(action.equals(BluetoothDevice.ACTION_FOUND)){
                //如果扫描时发现蓝牙设备,取到发现的设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);           
            }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
                //如果扫描完毕                
            }
        }
    };
  1. 广播过滤、注册、注销:
...
        //为广播接收器注册过滤器
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mBluetoothReceiver,filter);
...
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBluetoothReceiver);
    }

demo(查找已配对蓝牙,log打印出来):

续上,修改activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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:id="@+id/bt_paired_device"
        android:text="已配对设备"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bt_scan"
        android:text="扫描附近蓝牙设备"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
</LinearLayout>

接着修改MainActivity.java:
实例化、绑定:

...
//scan paired
    private Button mScanButton;
    private  Button mPairedDeviceButton;
...
        //scan paired
        mPairedDeviceButton = (Button)findViewById(R.id.bt_paired_device);
        mScanButton = (Button)findViewById(R.id.bt_scan);
...

添加onClick:

mPairedDeviceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //paired 配对的
                Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                //If threre are paired devices
//                if (pairedDevices.size() > 0) {
                    //Loop through paired devices
                    for(BluetoothDevice device : pairedDevices){
                        //You add the name and address to an array adapter to show in a ListView at this
                        Log.d(TAG, "Device name  " + device.getName());//打印匹配过的蓝牙设备的name
                        Log.d(TAG, "Device addr  " + device.getAddress());
                    }
//                }
            }
        });
        mScanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mBluetoothAdapter.isDiscovering()){//如果正在扫描,令之停止扫描,重新开始扫描
                    mBluetoothAdapter.cancelDiscovery();
                }
                mBluetoothAdapter.startDiscovery();//异步函数
            }
        });

运行之后点击“已配对设备”按钮,显示已配对蓝牙设备的信息:
9125154-ae31c90b70153aad.png

此时java:

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "BluetoothChat";
    private static final int REQUEST_ENABLE_BT = 10;//其是需要自己定义的局部常量。
    private BluetoothAdapter mBluetoothAdapter;

    //scan paired
    private Button mScanButton;
    private  Button mPairedDeviceButton;

    //广播接收

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

        //scan paired
        mPairedDeviceButton = (Button)findViewById(R.id.bt_paired_device);
        mScanButton = (Button)findViewById(R.id.bt_scan);
        mPairedDeviceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //paired 配对的
                Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                //If threre are paired devices
//                if (pairedDevices.size() > 0) {
                    //Loop through paired devices
                    for(BluetoothDevice device : pairedDevices){
                        //Add the name and address to an array adapter to show in a ListView
                        Log.d(TAG, "Device name  " + device.getName());//打印匹配过的蓝牙设备的name
                        Log.d(TAG, "Device addr  " + device.getAddress());
                    }
//                }
            }
        });
        mScanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mBluetoothAdapter.isDiscovering()){//如果正在扫描,令之停止扫描,重新开始扫描
                    mBluetoothAdapter.cancelDiscovery();
                }
                mBluetoothAdapter.startDiscovery();//异步函数
            }
        });

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(mBluetoothAdapter == null){
            //Device does not support Bluetooth
            Log.e(TAG, "Device does not support Bluetooth");
        }else {
            Toast.makeText(this,"设备支持蓝牙!",Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    protected void onStart() {
...
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
...
        }
    }
}

demo(续上,监测扫描发现设备时扫描完毕时两个状态,做对应处理):

  • 点击“扫描附近蓝牙设备”按钮时,开始一轮新的扫描:
        mScanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mBluetoothAdapter.isDiscovering()){//如果正在扫描,令之停止扫描,重新开始扫描
                    mBluetoothAdapter.cancelDiscovery();
                }
                mBluetoothAdapter.startDiscovery();//异步函数
            }
        });
  • 注册广播接收器,
    监测扫描发现设备时扫描完毕时两个状态,
    然后做对应处理:
    //广播接收
    private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();//获取action
            Log.d(TAG, "ACTION:" + action);
            if(action.equals(BluetoothDevice.ACTION_FOUND)){
                //如果扫描时发现蓝牙设备,取到发现的设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG, "new Device name  " + device.getName());//打印匹配过的蓝牙设备的name
                Log.d(TAG, "new Device addr  " + device.getAddress());
            }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
                //如果扫描完毕
                Toast.makeText(MainActivity.this,"Discovery done!",Toast.LENGTH_SHORT).show();
                Log.d(TAG, "Discovery done!");
            }
        }
    };
  • 在onCreate()末尾注册intent过滤器:
        //为广播接收器注册过滤器
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mBluetoothReceiver,filter);
  • 在onDestroy()中注销:
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBluetoothReceiver);
    }
  • 运行效果如图,没有发现新设备,在扫描完毕后打印出对应的Log:
    9125154-8e57d6cf16fe38be.png
  • 此时java:

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "BluetoothChat";
    private static final int REQUEST_ENABLE_BT = 10;//其是需要自己定义的局部常量。
    private BluetoothAdapter mBluetoothAdapter;

    //scan paired
    private Button mScanButton;
    private  Button mPairedDeviceButton;

    //广播接收
    private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();//获取action
            Log.d(TAG, "ACTION:" + action);
            if(action.equals(BluetoothDevice.ACTION_FOUND)){
                //如果扫描时发现蓝牙设备,取到发现的设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG, "new Device name  " + device.getName());//打印匹配过的蓝牙设备的name
                Log.d(TAG, "new Device addr  " + device.getAddress());
            }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
                //如果扫描完毕
                Toast.makeText(MainActivity.this,"Discovery done!",Toast.LENGTH_SHORT).show();
                Log.d(TAG, "Discovery done!");
            }
        }
    };

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

        //scan paired
        mPairedDeviceButton = (Button)findViewById(R.id.bt_paired_device);
        mScanButton = (Button)findViewById(R.id.bt_scan);
        mPairedDeviceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //paired 配对的
                Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                //If threre are paired devices
//                if (pairedDevices.size() > 0) {
                    //Loop through paired devices
                    for(BluetoothDevice device : pairedDevices){
                        //Add the name and address to an array adapter to show in a ListView
                        Log.d(TAG, "Device name  " + device.getName());//打印匹配过的蓝牙设备的name
                        Log.d(TAG, "Device addr  " + device.getAddress());
                    }
//                }
            }
        });
        mScanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mBluetoothAdapter.isDiscovering()){//如果正在扫描,令之停止扫描,重新开始扫描
                    mBluetoothAdapter.cancelDiscovery();
                }
                mBluetoothAdapter.startDiscovery();//异步函数
            }
        });

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(mBluetoothAdapter == null){
            //Device does not support Bluetooth
            Log.e(TAG, "Device does not support Bluetooth");
        }else {
            Toast.makeText(this,"设备支持蓝牙!",Toast.LENGTH_SHORT).show();
        }

        //为广播接收器注册过滤器
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mBluetoothReceiver,filter);
    }

    @Override
    protected void onStart() {
        super.onStart();

        if(!mBluetoothAdapter.isEnabled()){
            //向系统请求开启蓝牙
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);//结果返回回调到onActivityResult()
        }else {
            //已经开启了蓝牙
            Toast.makeText(this,"蓝牙已经开启",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_ENABLE_BT){
            Toast.makeText(this,"蓝牙已经开启",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBluetoothReceiver);
    }
}





参考自,慕课网。就业班

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在Java中,@RequestBody和@RequestParam是用于处理HTTP请求参数的注解。它们之间有一些区别,下面详细解析一下: 1. @RequestBody注解用于获取请求体中的参数,并将其绑定到方法的参数上。通常用于处理POST请求中的表单数据或者JSON数据。当使用@RequestBody注解时,Spring框架会自动将请求体中的数据转换为方法参数所需的对象。例如: ```java @PostMapping("/user") public void addUser(@RequestBody User user) { // 处理user对象 } ``` 在上面的例子中,@RequestBody注解将请求体中的数据转换为User对象,并将其绑定到addUser方法的参数上。 2. @RequestParam注解用于获取URL中的请求参数,并将其绑定到方法的参数上。通常用于处理GET请求或者POST请求中的URL参数。当使用@RequestParam注解时,Spring框架会自动从URL中提取指定名称的参数,并将其转换为方法参数所需的类型。例如: ```java @GetMapping("/user") public void getUser(@RequestParam("id") int userId) { // 处理userId参数 } ``` 在上面的例子中,@RequestParam注解将URL中名为"id"的参数提取出来,并将其转换为int类型,并将其绑定到getUser方法的参数userId上。 总结起来,@RequestBody注解用于获取请求体中的参数,适用于处理POST请求中的表单数据或者JSON数据。而@RequestParam注解用于获取URL中的请求参数,适用于处理GET请求或者POST请求中的URL参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

凌川江雪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值