• 浅谈Bluetooth蓝牙开发


    前言:项目用到蓝牙开发,具体蓝牙获取硬件传感器中的数据。

    因为没有蓝牙开发的相关经验,决定先了解一些蓝牙开发的知识,再去看之前同事写的蓝牙相关代码。

    ---------------------------------------------------------------------------------------------------

    一、蓝牙开发相关类

    1、BluetoothAdapter

     显而易见,蓝牙适配器。

     通过这个类我们进行以下操作:

    1、开关蓝牙设备
    2、扫描蓝牙设备
    3、设置/获取蓝牙状态信息,例如:蓝牙状态值、蓝牙Name、蓝牙Mac地址等;

    2、BluetoothDevice

    蓝牙设备,是我们连接的设备

    获取方法:

    BluetoothData.SENSOR_DOWN_ADRESS = “20:16:05:25:32:31”; //MAC地址
    BluetoothDevice sensor_down = mBluetoothAdapter.getRemoteDevice(BluetoothData.SENSOR_DOWN_ADRESS);

    ---------------------------------------------------------------------------------------------------

    二、蓝牙开发使用的基本步骤

    1、权限

    这个在AndroidManifest.xml文件中添加权限,这个是必须要的

        //在程序中使用蓝牙功能
        <uses-permission android:name="android.permission.BLUETOOTH"/>
        //启动设备发现或操纵蓝牙设置
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    2、获得蓝牙适配器 BluetoothAdapter 对象 并根据获得结果判断当前设备是否支持蓝牙

    BluetoothAdapter mBluetoothAdapter =  BluetoothAdapter.getDefaultAdapter();
    if(mBluetoothAdapter == null){
    //设备不支持蓝牙功能
    Toast.makeText(this,"当前设备不支持蓝牙功能",Toast.LENGTH_SHORT).show();
    return ;
    }

    3、在设备支持蓝牙功能的情况下,我们需要判断蓝牙功能是否开启,若没开启,需给之开启

    isEnabled()判断是否打开蓝牙功能,enable()方法用于打开蓝牙功能

    if(!mBluetoothAdapter.isEnabled()){
                boolean enable = mBluetoothAdapter.enable(); //返回值表示 是否成功打开了蓝牙功能
                if(enable){
                    Toast.makeText(this,"打开蓝牙功能成功!",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this,"打开蓝牙功能失败,请到'系统设置'中手动开启蓝牙功能!",Toast.LENGTH_SHORT).show();
                    return ;
                }
            }

    4、查询已经配对的蓝牙设备

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            //如果有配对的设备
            if(pairedDevices.size() > 0){
                ArrayList<String> mArrayAdapter = new ArrayList<>();
                for(BluetoothDevice device : pairedDevices){
                    //通过array adapter在列表中添加设备名称和地址
                    mArrayAdapter.add(device.getName() + "
    " + device.getAddress());
                    Log.i("bluetooth",device.getName() + "
    " + device.getAddress());
                }
            }else{
                Toast.makeText(this,"暂无已配对设备",Toast.LENGTH_SHORT).show();
            }

    5、查询附近可用的蓝牙设备

    方法很简单,就一句话

    mBluetoothAdapter.startDiscovery();

    注意此方法是异步执行的,相当于开启一个子线程,整个过程大约耗时12秒。

    切记,当我们搜索并成功连接到我们需要的设备的时候,需要及时的关闭搜索行为,可以使用cancelDiscovery。

    接下来我们需要写一个广播来接收查询到的设备数据

    private final BroadcastReceiver mReceiver = new BroadcastReceiver(){
            public void onReceive(Context context, Intent intent){
                String action = intent.getAction();
                allBlueToothDevice = new ArrayList<>();
                if(BluetoothDevice.ACTION_FOUND.equals(action)){
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    Log.i("bluetooth","可配对设备:"+device.getName() + "
    " + device.getAddress());
                }
            }
        };

    记得在onCreate()方法中注册广播:

     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
     registerReceiver(mReceiver, filter);

    相关知识:

    Android项目实战(二十五):蓝牙连接硬件设备开发规范流程

  • 相关阅读:
    16位汇编第三讲 分段存储管理思想
    16位汇编语言第二讲系统调用原理,以及各个寄存器详解
    /bin/sh 与 /bin/bash 的区别
    Linux中cat、more、less、tail、head命令的区别
    Linux之特殊权限(SUID/SGID/SBIT)
    HTML页面参数的传递与获取
    Ajax的跨域请求——JSONP的使用
    IDEA新建maven项目
    IDEA新建Web项目
    权限管理基础——原理与解决方案
  • 原文地址:https://www.cnblogs.com/xqxacm/p/5885630.html
Copyright © 2020-2023  润新知