Bluetooth LE(低功耗蓝牙) - 第六部分(完)

原文地址: http://blog.csdn.net/likebamboo/article/details/27485987


回顾:

     在本系列前面的文章中我们已经了解了,在我们从一个TI SensorTag中获取温度和湿度数据之前,我们需要经历的各种步骤。在本系列中的最后一篇文章,我们将完成注册并接收SensorTag的通知,并接收温度和湿度数据。

接收数据:

     现在,本地的代理组件知道了传感器所提供的服务,我们可以开始使用这些服务了。为了使用它们,我们首先需要获得服务,然后是该服务所包含的特征,最后是特征的描述符。

     一个GATT服务表现为一个 BluetoothGattService 对象,我们需要通过适当的UUID从 BluetoothGatt 实例中获得;一个GATT特征表示为一个 BluetoothGattCharacteristic  对象,我们可以通过适当的UUID从 BluetoothGattService 中得到;一个GATT描述符表现为一个 BluetoothGattDescriptor 对象,我们可以通过适当的UUID从BluetoothGattCharacteristic  对象中获得:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private static final UUID UUID_HUMIDITY_SERVICE =   
  2.     UUID.fromString("f000aa20-0451-4000-b000-000000000000");  
  3. private static final UUID UUID_HUMIDITY_DATA =   
  4.     UUID.fromString("f000aa21-0451-4000-b000-000000000000");  
  5. private static final UUID UUID_HUMIDITY_CONF =   
  6.     UUID.fromString("f000aa22-0451-4000-b000-000000000000");  
  7. private static final UUID UUID_CCC =   
  8.     UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");  
  9. .  
  10. .  
  11. .  
  12. private void subscribe(BluetoothGatt gatt) {  
  13.     BluetoothGattService humidityService =   
  14.         gatt.getService(UUID_HUMIDITY_SERVICE);  
  15.     if(humidityService != null) {  
  16.         BluetoothGattCharacteristic humidityCharacteristic =   
  17.             humidityService.getCharacteristic(UUID_HUMIDITY_DATA);  
  18.         BluetoothGattCharacteristic humidityConf =   
  19.             humidityService.getCharacteristic(UUID_HUMIDITY_CONF);  
  20.         if(humidityCharacteristic != null && humidityConf != null) {  
  21.             BluetoothGattDescriptor config =   
  22.                 humidityCharacteristic.getDescriptor(UUID_CCC);  
  23.             if(config != null) {  
  24.             }  
  25.         }  
  26.     }  
  27. }  

      为了使示例代码清晰,我已经移除了所有的错误信息展示代码,但在实际的应用程序中你应该考虑如何将错误信息通知给用户。

      从 UUID_HUMIDITY_DATA  特征中读取数据一件简单的事,只需调用gatt.readCharacteristic(humidityCharacteristic) 方法,并在BluetoothGattCallback  的实现类上复写onReadCharacteristic  方法(再次说明,这是一个异步执行的操作,所以可以在UI线程上调用readCharacteristic() 方法)。这样一次性的操作是没什么问题的,但我们应用程序要不断监测温度和湿度值,并将其报告给用户。相比注册监听特征值的发生,通过调用readCharacteristic() 定期轮询会更有效(译者注:感觉说反了,大家可以看看原文,不知道是不是我翻译错了: Instead we can get notifications whenever the value changes, which is much more efficient that polling and performing a readCharacteristic() periodically.)。

      注册监听特征值变化的通知我们要做三件事。首先我们必须把传感器打开,使SensorTag 开始收集适当的数据(实际上如果我们使用readCharacteristic()  方法,同样要打开传感器)。然后我们必须注册传感器以及我们需要通知的本地代理。这一步是特别针对于SensorTag的,其他设备可能不需要这样,或者他们可能需要另一种方法。

      为了打开传感器,我们必须写一个 0×01 值到 UUID_HUMIDITY_CONF 特性;为了收到通知,我们首先必须调用BluetoothGatt  实例的setCharacteristicNotification()  方法,然后写一个 {0x00,0x01}(两个字节)的值到 UUID_CCC 描述符。正如我们前面所提到的那样,UUID_CCC描述符不是SensorTag特定的(就像其他所有的UUID一样),这是一个标准关闭或打开通知的UUID。

      到目前为止,我们已经极大地受益于BLE的异步性API,因为我们不需要担心从UI线程调用它。不幸的是,它使我们面临一些小问题。我们希望下面的代码会做我们所要求的事,但是运行它不会开始收到通知:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. gatt.setCharacteristicNotification(humidityCharacteristic, true);  
  2. humidityConf.setValue(ENABLE_SENSOR);  
  3. gatt.writeCharacteristic(humidityConf);  
  4. config.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);  
  5. gatt.writeDescriptor(config);  

     原因是我们项传感器写入值的操作是异步执行的,那不会起作用,我们必须等待一个写操作完后才可以开始下一个。如果我们不能等待,在第一个值正在写入的过程中尝试写入第二个值,那么第二值就会丢失。要克服他并不困难,我们可以维护一个写队列,当我们收到前一个写操作已经完成的通知后触发下一个写操作:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private static final byte[] ENABLE_SENSOR = {0x01};  
  2. private static final Queue<Object> sWriteQueue =   
  3.     new ConcurrentLinkedQueue<Object>();  
  4. private static boolean sIsWriting = false;  
  5.   
  6. private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {  
  7.     .  
  8.     .  
  9.     .  
  10.     @Override  
  11.     public void onCharacteristicWrite(BluetoothGatt gatt,   
  12.         BluetoothGattCharacteristic characteristic,   
  13.         int status) {  
  14.         Log.v(TAG, "onCharacteristicWrite: " + status);  
  15.         sIsWriting = false;  
  16.         nextWrite();  
  17.     }  
  18.   
  19.     @Override  
  20.     public void onDescriptorWrite(BluetoothGatt gatt,   
  21.         BluetoothGattDescriptor descriptor,   
  22.         int status) {  
  23.         Log.v(TAG, "onDescriptorWrite: " + status);  
  24.         sIsWriting = false;  
  25.         nextWrite();  
  26.     }  
  27. }  
  28. .  
  29. .  
  30. .  
  31. private void subscribe(BluetoothGatt gatt) {  
  32.     BluetoothGattService humidityService =   
  33.         gatt.getService(UUID_HUMIDITY_SERVICE);  
  34.     if(humidityService != null) {  
  35.         BluetoothGattCharacteristic humidityCharacteristic =   
  36.             humidityService.getCharacteristic(UUID_HUMIDITY_DATA);  
  37.         BluetoothGattCharacteristic humidityConf =   
  38.             humidityService.getCharacteristic(UUID_HUMIDITY_CONF);  
  39.         if(humidityCharacteristic != null &&   
  40.             humidityConf != null) {  
  41.             BluetoothGattDescriptor config =   
  42.                 humidityCharacteristic.getDescriptor(UUID_CCC);  
  43.             if(config != null) {  
  44.                 gatt.setCharacteristicNotification(  
  45.                     humidityCharacteristic, true);  
  46.                 humidityConf.setValue(ENABLE_SENSOR);  
  47.                 write(humidityConf);  
  48.                 config.setValue(  
  49.                     BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);  
  50.                 write(config);  
  51.             }  
  52.         }  
  53.     }  
  54. }  
  55.   
  56. private synchronized void write(Object o) {  
  57.     if(sWriteQueue.isEmpty() && !sIsWriting) {  
  58.         doWrite(o);  
  59.     } else {  
  60.         sWriteQueue.add(o);  
  61.     }  
  62. }  
  63.   
  64. private synchronized void nextWrite() {  
  65.     if(!sWriteQueue.isEmpty() && !sIsWriting) {  
  66.         doWrite(sWriteQueue.poll());  
  67.     }  
  68. }  
  69.   
  70. private synchronized void doWrite(Object o) {  
  71.     if(o instanceof BluetoothGattCharacteristic) {  
  72.         sIsWriting = true;  
  73.         mGatt.writeCharacteristic(  
  74.             (BluetoothGattCharacteristic)o);  
  75.     } else if(o instanceof BluetoothGattDescriptor) {  
  76.         sIsWriting = true;  
  77.         mGatt.writeDescriptor((BluetoothGattDescriptor) o);  
  78.     } else {  
  79.         nextWrite();  
  80.     }  
  81. }  

      所以在加入这个简单的写队列之后,我们只需要在我们的BluetoothGattCallback  实现类中加入相应的回调方法以开启监听通知事件。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {  
  2.     .  
  3.     .  
  4.     .  
  5.     @Override  
  6.     public void onCharacteristicChanged(BluetoothGatt gatt,   
  7.         BluetoothGattCharacteristic characteristic) {  
  8.         Log.v(TAG, "onCharacteristicChanged: " +   
  9.             characteristic.getUuid());  
  10.         if(characteristic.getUuid().equals(UUID_HUMIDITY_DATA)) {  
  11.             int t = shortUnsignedAtOffset(characteristic, 0);  
  12.             int h = shortUnsignedAtOffset(characteristic, 2);  
  13.             t = t - (t % 4);  
  14.             h = h - (h % 4);  
  15.   
  16.             float humidity = (-6f) + 125f * (h / 65535f);  
  17.             float temperature = -46.85f +   
  18.                 175.72f/65536f * (float)t;  
  19.             Log.d(TAG, "Value: " + humidity + ":" + temperature);  
  20.             Message msg = Message.obtain(null, MSG_DEVICE_DATA);  
  21.             msg.arg1 = (int)(temperature * 100);  
  22.             msg.arg2 = (int)(humidity * 100);  
  23.             sendMessage(msg);  
  24.         }  
  25.     }  
  26. }  
  27.   
  28. private static Integer shortUnsignedAtOffset(  
  29.     BluetoothGattCharacteristic characteristic, int offset) {  
  30.     Integer lowerByte = characteristic.getIntValue(  
  31.         BluetoothGattCharacteristic.FORMAT_UINT8, offset);  
  32.     Integer upperByte = characteristic.getIntValue(  
  33.         BluetoothGattCharacteristic.FORMAT_UINT8, offset + 1);   
  34.   
  35.     return (upperByte << 8) + lowerByte;  
  36. }  

     GATT特征中的数据包括温度值(位于第0个字节和第1个字节)和湿度值(位于第2个字节和第3个字节)。然后我们去掉任何不必要的状态位。计算方法直接来自 SensorTag 文档 

      为了将这些数据展示在界面上,我们使用 Message 的 arg1和arg2 两个成员变量,在将其转换为所需的Int值之前我们先乘100。之后,我们将除以100以返回一个float值。就像以前一样,在此不会展示UI的代码,但在源代码中会有。

      如果我们将程序运行起来,我们可以看到显示处理的摄氏温度以及湿度百分比 【视频在youtube 上,需要穿越才能看到哦

      http://www.youtube.com/embed/dyr01b2iy-I?version=3&rel=1&fs=1&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent

下期预告:

     所以现在我们完成了一个拥有我们想要功能的应用,但这个博客的主题是Styling Android,因此下周我们将开始一个新的系列,我们以这个应用程序作为基础,并真正的让该程序的UX(用户体验)更平滑一点,UI(用户界面)看起来好一点!

     

     本文的源代码在这里 可以找到

-----------------------------------------------------不怎么华丽的分割线----------------------------------------------------------------------

原文链接:http://blog.stylingandroid.com/archives/2531

© 2014, Mark Allison. All rights reserved. This article originally appeared on Styling Android.

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License   


OK,大功告成,感谢CCTV ,感谢MTV ,当然还得感谢本系列文章的原作者Mark Allison 。。。



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值