前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android蓝牙详析 | 蓝牙的适配、权限、开启、搜索发现等处理

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

作者头像
凌川江雪
发布2019-05-17 14:53:04
5.6K0
发布2019-05-17 14:53:04
举报
文章被收录于专栏:李蔚蓬的专栏李蔚蓬的专栏

本系列笔记概述

  • 蓝牙传输优势:功耗低,传输距离还可以;
  • 蓝牙聊天室案例
  • Android中蓝牙设备的使用
    • 蓝牙权限(本文的讲解内容之一)
    • 蓝牙功能开启(本文的讲解内容之一)
    • 搜索蓝牙设备(本文的讲解内容之一)
    • 与外设搭建RFCOMM通道(射频通道)
    • 蓝牙设备双向数据传输

蓝牙聊天室案例框架

  • 蓝牙权限
    • 执行蓝牙通信需要权限BLUETOOTH, 例如:请求连接、接收连接和传输数据等;
    • 如果需要启动设备 或 操作蓝牙设置,则需声明BLUETOOTH_ADMIN权限
代码语言:javascript
复制
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  • 设置蓝牙——获取BlueAdapter
    • 使用蓝牙需用到BlueAdapter。表示设备自身的蓝牙适配器;
    • 通过静态方法BlueAdapter.getDefaultAdapter()获得BlueAdapter;
    • 整个系统只有一个蓝牙适配器,application可使用此BlueAdapter对象与之交互;
    • 如果getDefaultAdapter()返回null,则表示该设备不支持蓝牙, 例如:
代码语言:javascript
复制
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(mBluetoothAdapter == null){
            //Device does not support Bluetooth
        }
  • 启用蓝牙
    • 调用isEnable()以检查当前是否已启用蓝牙; 如果此方法返回false,则表示蓝牙处于停用状态;
    • 要请求启用蓝牙,将通过ACTION_REQUEST_ENABLE向系统设置 发出启用蓝牙的请求(无需停止应用), 例如:
代码语言:javascript
复制
...
    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:
代码语言:javascript
复制
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
代码语言:javascript
复制
 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. 广播接收:
代码语言:javascript
复制
 //广播接收
    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. 广播过滤、注册、注销:
代码语言:javascript
复制
...
        //为广播接收器注册过滤器
        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:

代码语言:javascript
复制
<?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: 实例化、绑定:

代码语言:javascript
复制
...
//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:

代码语言:javascript
复制
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();//异步函数
            }
        });

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

此时java:

代码语言:javascript
复制
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(续上,监测扫描发现设备时扫描完毕时两个状态,做对应处理):

  • 点击“扫描附近蓝牙设备”按钮时,开始一轮新的扫描:
代码语言:javascript
复制
        mScanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mBluetoothAdapter.isDiscovering()){//如果正在扫描,令之停止扫描,重新开始扫描
                    mBluetoothAdapter.cancelDiscovery();
                }
                mBluetoothAdapter.startDiscovery();//异步函数
            }
        });
  • 注册广播接收器, 监测扫描发现设备时扫描完毕时两个状态, 然后做对应处理:
代码语言:javascript
复制
    //广播接收
    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过滤器:
代码语言:javascript
复制
        //为广播接收器注册过滤器
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mBluetoothReceiver,filter);
  • 在onDestroy()中注销:
代码语言:javascript
复制
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBluetoothReceiver);
    }
  • 运行效果如图,没有发现新设备,在扫描完毕后打印出对应的Log:
  • 此时java:
代码语言:javascript
复制
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);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本系列笔记概述
  • 蓝牙聊天室案例框架
    • demo(查看本机是否支持蓝牙,蓝牙是否开启,没开启则请求):
      • demo(查找已配对蓝牙,log打印出来):
        • demo(续上,监测扫描发现设备时、扫描完毕时两个状态,做对应处理):
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档