Android开发之Bluetooth蓝牙开发
白羽 2018-06-27 来源 :网络 阅读 1260 评论 0

摘要:本文将带你了解Android开发之Bluetooth蓝牙开发,希望本文对大家学Android有所帮助。


 



一、蓝牙开发相关类

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() + "\n" + device.getAddress());

                Log.i("bluetooth",device.getName() + "\n" + 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() + "\n" + device.getAddress());

            }

        }

    };

 

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

 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

 registerReceiver(mReceiver, filter);

 


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved