本篇博文最后修改时间:2017年01月21日,15:38。
一、简介
本文介绍如何在安卓工程中启动蓝牙。
二、实验平台
开发平台:Eclipse IDE for Java Developers(Version: Luna Service Release 2 (4.4.2))
手机:红米1S
三、版权声明
博主:甜甜的大香瓜
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.csdn.NET/feilusia
联系方式:897503845@qq.com
香瓜BLE之CC2541群:127442605
香瓜BLE之CC2640群:557278427
香瓜单片机之STM8/STM32群:164311667
甜甜的大香瓜的小店(淘宝店):https://shop217632629.taobao.com/?spm=2013.1.1000126.d21.hd2o8i
四、
实验前提
1、在进行本文步骤前,请先
阅读
以下博文:
暂无
2、在进行本文步骤前,请先
实现以下博文:
1)《Android(BLE)之蓝牙权限和feature》:http://blog.csdn.net/feilusia/article/details/54645998#userconsent#
五、基础知识
1、蓝牙启动过程是如何的?
答:
1)获取BluetoothAdapter:BluetoothAdapter对应本地安卓设备的蓝牙模块。
2)判断是否支持BLE。
3)如果支持BLE,则打开蓝牙。
六、实验步骤
1、添加所需变量(在GUA_BLE_Activity.java的GUA_BLE_Activity中)
private static BluetoothManager mBluetoothManager; //蓝牙设备管理器
private BluetoothAdapter mBluetoothAdapter = null; //蓝牙适配器
2、添加启动BLE的代码(在GUA_BLE_Activity.java的GUA_BLE_Activity的onCreate中)
//获取安卓的蓝牙模块的管理权限
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
//判断本台设备是否支持BLE
if (mBluetoothAdapter == null) {
Toast.makeText(this, "ble not support", Toast.LENGTH_LONG).show();
mBleSupported = false;
}
//打开蓝牙
if (!mBluetoothAdapter.isEnabled())
{
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
七、注意事项
暂无
八、实验结果
编译工程并下载apk到手机中测试,当打开apk时会弹窗提示要求启动蓝牙,手动点确定后则启动成功,如下图:
1)
2)