Android BLE蓝牙4.0开发详解

Android BLE蓝牙4.0开发详解这篇博客主要讲解蓝牙 BLE 的用法 在讲解之前先讲一些概念性的东西 对于之前没接触过蓝牙开发 现在手上又有个蓝牙 BLE 项目需要做的人 先看下这些概念还是很重要的 因为我之前就是这样 之前没有接触过蓝牙方面的开发 然后来了个蓝牙的项目 于是就到网上百度了一番 于是有点茫然 产生了几点疑惑 1 发现蓝牙有传统蓝牙和低功耗蓝牙 ble 之分 那么什么是传

          这篇博客主要讲解 蓝牙 BLE 的用法。在讲解之前先讲一些概念性的东西,对于之前没接触过蓝牙开发,现在手上又有个蓝牙BLE项目需要做的人,先看下这些概念还是很重要的。因为我之前就是这样,之前没有接触过蓝牙方面的开发,然后来了个蓝牙的项目,于是就到网上百度了一番,于是有点茫然,产生了几点疑惑:
            1:发现蓝牙有传统蓝牙和低功耗蓝牙(ble)之分。那么什么是传统蓝牙,什么又是低功耗蓝牙?之前又没做过蓝牙开发,我该用哪种方式去开发我这个项目?用最新的 方式的话,传统方式蓝牙开发我是不是该要先了解?
            2:蓝牙到底有哪些版本?哪些版本称为传统蓝牙?哪些版本称为低功耗蓝牙?           
            3:传统蓝牙和低功耗蓝牙有什么区别?为什么低功耗蓝牙的出现使得智能能穿戴越来越流行?


             BLE分为三部分:Service,Characteristic,Descriptor。这三部分都用UUID作为唯一标识符。UUID为这种格式:0000ffe1-0000-1000-8000-00805f9b34fb。比如有3个Service,那么就有三个不同的UUID与Service对应。这些UUID都写在硬件里,我们通过BLE提供的API可以读取到。

          一个BLE终端可以包含多个Service, 一个Service可以包含多个Characteristic,一个Characteristic包含一个value和多个Descriptor,一个Descriptor包含一个Value。Characteristic是比较重要的,是手机与BLE终端交换数据的关键,读取设置数据等操作都是操作Characteristic的相关属性。

            比如我有个BLE的硬件,我们可以用android 版本的light blue去连接上这个硬件,没有的话我文章底部提供了下载链接,不过它在android5.0以上跑不起来,大家也可以下载另外两个源码跑起来也可以连接上,不过这个android 版本的light blue最好用了。进入应用,就可以扫描到你的BLE设备,点击就会连接上,然后我们可以看到UUID列表,这里每一行的UUID都代表一个Service,再点击任意一行进去,又可以看到一个UUID列表,这里每一行的UUID都代表一个Characteristic,再点击任意一行进去,即可以操作这个Characteristic,比如写入数据或者读出数据等。

            好了,那我们来根据代码讲解下吧。代码有上传,里面有Bluetooth4_3/BLEDemo/Android_Lightblue.apk。下面讲解的是根据代码Bluetooth4_3来讲解的。

            1:首先在程序里我们要开启蓝牙权限

在应用程序manifest文件中添加如下代码,声明蓝牙权限。

<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

如果你想声明你的应用程序只能在支持BLE的设备上运行,可以将下面声明包含进你的应用程序manifest文件中:

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"></uses-feature>

然而,如果你想让你的应用程序也能够在不支持BLE的设备上运行,你就应该将上面标签中的属性设置为required=”false”。然后在运行的过程中使用PackageManager.hasSystemFeature()方法来判断设备是否支持BLE:

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); }

             2:判断设备是否支持蓝牙ble

// 检查当前手机是否支持ble 蓝牙,如果不支持退出程序 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); }
final BluetoothManager bluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
//如果mBluetoothAdapter == null,说明设备不支持蓝牙

           4:弹出是否启用蓝牙的对话框

if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_ENABLE: if (resultCode == Activity.REQUEST_ENABLE_BT) { Toast.makeText(this, "蓝牙已启用", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "蓝牙未启用", Toast.LENGTH_SHORT).show(); } break; } }
//也可以直接调用mBluetoothAdapter.enable()mBluetoothAdapter.disable()来启用禁用蓝牙。不过这种方式不会弹出询问对话框
 

            5:搜索设备

 private void scanLeDevice(final boolean enable) { if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Override public void run() { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); } }, SCAN_PERIOD); //10秒后停止搜索 mScanning = true; mBluetoothAdapter.startLeScan(mLeScanCallback); //开始搜索 } else { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback);//停止搜索 } } //启动搜索的操作最好放在Activity的onResume里面或者服务里面,我有发现放在onCreate有时响应不及时

        6:搜索到设备会回调LeScanCallback接口

 private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { runOnUiThread(new Runnable() { @Override public void run() { //在这里可以把搜索到的设备保存起来 //device.getName();获取蓝牙设备名字 //device.getAddress();获取蓝牙设备mac地址 } }); } };

        7:选择一个设备进行连接。连接后会返回一个BluetoothGatt 类型的对象,这里定义为mBluetoothGatt。该对象比较重要,后面发现服务读写设备等操作都是通过该对象。
代码里建了一个service,里面封装了连接,读写设备等操作。连接是通过获取到的mac地址去进行连接操作就可以了。

 public boolean connect(final String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG,"BluetoothAdapter not initialized or unspecified address."); return false; } // Previously connected device. Try to reconnect. (先前连接的设备。 尝试重新连接) if (mBluetoothDeviceAddress != null&& address.equals(mBluetoothDeviceAddress)&& mBluetoothGatt != null) { Log.d(TAG,"Trying to use an existing mBluetoothGatt for connection."); if (mBluetoothGatt.connect()) { mConnectionState = STATE_CONNECTING; return true; } else { return false; } } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); return false; } // We want to directly connect to the device, so we are setting the // autoConnect // parameter to false. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); //该函数才是真正的去进行连接 Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; mConnectionState = STATE_CONNECTING; return true; }

          8:连接后会回调BluetoothGattCallback接口,包括读取设备,往设备里写数据及设备发出通知等都会回调该接口。其中比较重要的是BluetoothGatt。

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override //当连接上设备或者失去连接时会回调该函数 public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { //连接成功 mBluetoothGatt.discoverServices(); //连接成功后就去找出该设备中的服务 private BluetoothGatt mBluetoothGatt; } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { //连接失败 } } @Override //当设备是否找到服务时,会回调该函数 public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { //找到服务了 //在这里可以对服务进行解析,寻找到你需要的服务 } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override //当读取设备时会回调该函数 public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) { System.out.println("onCharacteristicRead"); if (status == BluetoothGatt.GATT_SUCCESS) { //读取到的数据存在characteristic当中,可以通过characteristic.getValue();函数取出。然后再进行解析操作。 //int charaProp = characteristic.getProperties();if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0)表示可发出通知。 判断该Characteristic属性 } } @Override //当向设备Descriptor中写数据时,会回调该函数 public void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) { System.out.println("onDescriptorWriteonDescriptorWrite = " + status + ", descriptor =" + descriptor.getUuid().toString()); } @Override //设备发出通知时会调用到该接口 public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) { if (characteristic.getValue() != null) { System.out.println(characteristic.getStringValue(0)); } System.out.println("--------onCharacteristicChanged-----"); } @Override public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { System.out.println("rssi = " + rssi); } @Override //当向Characteristic写数据时会回调该函数 public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) { System.out.println("--------write success----- status:" + status); }; }

                9:设备连接成功并回调BluetoothGattCallback接口里面的onConnectionStateChange函数,然后调用mBluetoothGatt.discoverServices();去发现服务。发现服务后会回调BluetoothGattCallback接口里面的 onServicesDiscovered函数,在里面我们可以获取服务列表。

 public List<BluetoothGattService> getSupportedGattServices() { if (mBluetoothGatt == null) return null; return mBluetoothGatt.getServices(); //此处返回获取到的服务列表 }

                10:获取到服务列表后自然就是要对服务进行解析。解析出有哪些服务,服务里有哪些Characteristic,哪些Characteristic可读可写可发通知等等。

 private void displayGattServices(List<BluetoothGattService> gattServices) { if (gattServices == null) return; for (BluetoothGattService gattService : gattServices) { // 遍历出gattServices里面的所有服务 List<BluetoothGattCharacteristic> gattCharacteristics = gattServices.getCharacteristics(); for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { // 遍历每条服务里的所有Characteristic if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(需要通信的UUID)) { // 有哪些UUID,每个UUID有什么属性及作用,一般硬件工程师都会给相应的文档。我们程序也可以读取其属性判断其属性。 // 此处可以可根据UUID的类型对设备进行读操作,写操作,设置notification等操作 // BluetoothGattCharacteristic gattNoticCharacteristic 假设是可设置通知的Characteristic // BluetoothGattCharacteristic gattWriteCharacteristic 假设是可读的Characteristic // BluetoothGattCharacteristic gattReadCharacteristic 假设是可写的Characteristic } } } }
 

              11:可接收通知的UUID,设置其可以接收通知(notification)。下面函数参数为10中的gattNoticCharacteristic

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID .fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); if (descriptor != null) { System.out.println("write descriptor"); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }

              12:可读的UUID。下面函数参数为10中的gattReadCharacteristicreadCharacteristic调用成功会回调步骤8中的onCharacteristicRead函数

 public void readCharacteristic(BluetoothGattCharacteristic characteristic) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.readCharacteristic(characteristic); }

               13:可写的UUID。下面函数参数为10中的gattWriteCharacteristic。writeCharacteristic调用成功会回调步骤8中的onCharacteristicWrite函数

 public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.writeCharacteristic(characteristic); }

代码说明: 下面是源码下载。源码里面有Bluetooth4_3/BLEDemo/Android_Lightblue.apk三个.前两个是BLE的demo。我们上面讲解的是基于Bluetooth4_3这个demo讲解的。BLEDemo 这个功能较Bluetooth4_3多一些,有兴趣的可以都看下。Android_Lightblue.apk是Android版的lightblue,在进行ble开发的时候用该app作为辅助工具还是不错的,功能较Bluetooth4_3 BLEDemo 这两个demo都强大。不过Android_Lightblue.apk在android5.0以上的版本手机上运行不起来,我也没有该app的源码。看看后面会不会有更新

源码下载

Github下载

个人网站:https://www.leachchen.com/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232601.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • AIC和BIC准则详解

    AIC和BIC准则详解很多参数估计问题均采用似然函数作为目标函数,当训练数据足够多时,可以不断提高模型精度,但是以提高模型复杂度为代价,同时带来一个机器学习中非常普遍的问题——过拟合。所以,模型选择问题在模型复杂度与模型对数据集描述能力(即似然函数)之间寻求最佳平衡。人们提出许多信息准则,通过加入模型复杂度的惩罚项来避免过拟合问题,此处我们介绍一下常用的两个模型选择方法:1.赤池信息准则(AkaikeInformationCriterion,AIC)AIC是衡量统计模型拟合优良性的一种标准,由日本统计学家赤池弘次在

    2022年5月23日
    80
  • php裁剪图片(支持定点裁剪)

    php裁剪图片(支持定点裁剪)

    2021年11月4日
    46
  • Windows 技术篇-LDSGameMaster文件夹有什么用,删除方法

    Windows 技术篇-LDSGameMaster文件夹有什么用,删除方法LDS是鲁大师的拼写,应该是用过鲁大师,偷偷给你安装的。分析:没什么用,流氓程序,还很大占地方,4个G,可以放心的卸掉。卸载方法:找到里面的卸载程序来卸载,卸载完后把文件夹删除就好了。

    2022年6月14日
    69
  • 返回给前端的json,long转string

    返回给前端的json,long转stringpackagecom.zhidianfan.ydusermanager.config;importcom.fasterxml.jackson.databind.ObjectMapper;importcom.fasterxml.jackson.databind.module.SimpleModule;importcom.fasterxml.jackson.databind.ser.s…

    2022年5月14日
    60
  • 京东创始人是刘强东和谁_日本软银最大股东是谁

    京东创始人是刘强东和谁_日本软银最大股东是谁京东可以说是众所周知的一个购物平台,京东的自营可以说是给人们提供了很多方便性和可靠性。创始人刘强东靠着自己坚韧的毅力,以及卓越的非凡见识,一步一步地把京东做到了最强。可以说也是不容易啊,其中流下的辛

    2022年8月1日
    9
  • 浅析Promise用法[通俗易懂]

    浅析Promise用法[通俗易懂]浅析Promise用法要理解Promise要知道没有Promise的回调地狱如何插入一段漂亮的代码片Promise语法与then的用法所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果。从语法上说,Promise是一个对象,从它可以获取异步操作的消息。Promise提供统一的API,各种异步操作都可以用同样的方法进行处理。Promis…

    2022年6月19日
    28

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号