Android 蓝牙4.0 BLE 理解

本文简单结合两篇文章

http://blog.csdn.net/hellogv/article/details/24267685

http://blog.csdn.net/jimoduwu/article/details/21604215

在BLE协议中,有两个角色,周边(Periphery)和中央(Central),一个中央可以同时连接多个周边,但是一个周边某一时刻只能连接一个中央。但是不管是Periphery还是Central都是可以实现 GATT
server 和 GATT client去传输数据,但是无法同时都是。

大概了解了概念后,看看Android BLE SDK的四个关键类(class):

a) BluetoothGattServer作为周边来提供数据;BluetoothGattServerCallback返回周边的状态。

b) BluetoothGatt作为中央来使用和处理数据;BluetoothGattCallback返回中央的状态和周边提供的数据。

因为我们讨论的是Android的BLE SDK,下面所有的BluetoothGattServer代表周边,BluetoothGatt代表中央。

一.创建一个周边(虽然目前周边API在Android手机上不工作,但还是看看)

a)先看看周边用到的class,蓝色椭圆

b)说明:

每一个周边BluetoothGattServer,包含多个服务Service,每一个Service包含多个特征Characteristic。

1.new一个特征:character = new BluetoothGattCharacteristic(

UUID.fromString(characteristicUUID),

BluetoothGattCharacteristic.PROPERTY_NOTIFY,

BluetoothGattCharacteristic.PERMISSION_READ);

2.new一个服务:service = new BluetoothGattService(UUID.fromString(serviceUUID),

BluetoothGattService.SERVICE_TYPE_PRIMARY);

3.把特征添加到服务:service.addCharacteristic(character);

4.获取BluetoothManager:manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

5.获取/打开周边:BluetoothGattServer server = manager.openGattServer(this,

new BluetoothGattServerCallback(){...});

6.把service添加到周边:server.addService(service);

7.开始广播service:Google还没有广播Service的API,等吧!!!!!所以目前我们还不能让一个Android手机作为周边来提供数据。

二.创建一个中央(这次不会让你失望,可以成功创建并且连接到周边的)

a)先看看中央用到的class,蓝色椭圆

b)说明:

为了拿到中央BluetoothGatt,可要爬山涉水十八弯:

1.先拿到BluetoothManager:bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

2.再拿到BluetoothAdapt:btAdapter = bluetoothManager.getAdapter();

3.开始扫描:btAdapter.startLeScan( BluetoothAdapter.LeScanCallback);

4.从LeScanCallback中得到BluetoothDevice:public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {.....}

5.用BluetoothDevice得到BluetoothGatt:gatt = device.connectGatt(this, true, gattCallback);

终于拿到中央BluetoothGatt了,它有一堆方法(查API吧),调用这些方法,你就可以通过BluetoothGattCallback和周边BluetoothGattServer交互了。

官方有给出BLE 通信的sample ,下面是牛人简化了代码,简化得简单明了

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

最近穿戴设备发展得很火,把相关技术也带旺了,其中一项是BLE(Bluetooth
Low Energy
)。BLE是蓝牙4.0的核心Profile,主打功能是快速搜索,快速连接,超低功耗保持连接和传输数据,弱点是数据传输速率低,由于BLE的低功耗特点,因此普遍用于穿戴设备。Android 4.3才开始支持BLE API,所以请各位客官把本文代码运行在蓝牙4.0和Android 4.3及其以上的系统,另外本文所用的BLE终端是一个蓝牙4.0的串口蓝牙模块。

PS:我的i9100刷了4.4系统后,竟然也能跟BLE蓝牙模块通信。

BLE分为三部分Service、Characteristic、Descriptor,这三部分都由UUID作为唯一标示符。一个蓝牙4.0的终端可以包含多个Service,一个Service可以包含多个Characteristic,一个Characteristic包含一个Value和多个Descriptor,一个Descriptor包含一个Value。一般来说,Characteristic是手机与BLE终端交换数据的关键,Characteristic有较多的跟权限相关的字段,例如PERMISSION和PROPERTY,而其中最常用的是PROPERTY,本文所用的BLE蓝牙模块竟然没有标准的Characteristic的PERMISSION。Characteristic的PROPERTY可以通过位运算符组合来设置读写属性,例如READ|WRITE、READ|WRITE_NO_RESPONSE|NOTIFY,因此读取PROPERTY后要分解成所用的组合(本文代码已含此分解方法)。

本文代码改自Android 4.3 Sample的BluetoothLeGatt,把冗余代码去掉,获取的BLE设备信息都通过Log,还有一些必要的读写蓝牙方法,应该算是简化到大家一看就可以懂了。本文代码可以到http://download.csdn.net/detail/hellogv/7228819下载。接下来贴出本文运行的结果,首先是连接BLE设备后,枚举出设备所有Service、Characteristic、Descriptor,并且手机会往Characteristic uuid=0000ffe1-0000-1000-8000-00805f9b34fb写入“send
data->”字符串,BLE终端收到数据通过串口传到PC串口助手(见PC串口助手的截图):

04-21 18:28:25.465: E/DeviceScanActivity(12254): -->service type:PRIMARY

04-21 18:28:25.465: E/DeviceScanActivity(12254): -->includedServices size:0

04-21 18:28:25.465: E/DeviceScanActivity(12254): -->service uuid:00001800-0000-1000-8000-00805f9b34fb

04-21 18:28:25.465: E/DeviceScanActivity(12254): ---->char uuid:00002a00-0000-1000-8000-00805f9b34fb

04-21 18:28:25.465: E/DeviceScanActivity(12254): ---->char permission:UNKNOW

04-21 18:28:25.465: E/DeviceScanActivity(12254): ---->char property:READ

04-21 18:28:25.465: E/DeviceScanActivity(12254): ---->char uuid:00002a01-0000-1000-8000-00805f9b34fb

04-21 18:28:25.470: E/DeviceScanActivity(12254): ---->char permission:UNKNOW

04-21 18:28:25.470: E/DeviceScanActivity(12254): ---->char property:READ

04-21 18:28:25.470: E/DeviceScanActivity(12254): ---->char uuid:00002a02-0000-1000-8000-00805f9b34fb

04-21 18:28:25.470: E/DeviceScanActivity(12254): ---->char permission:UNKNOW

04-21 18:28:25.470: E/DeviceScanActivity(12254): ---->char property:READ|WRITE|

04-21 18:28:25.470: E/DeviceScanActivity(12254): ---->char uuid:00002a03-0000-1000-8000-00805f9b34fb

04-21 18:28:25.470: E/DeviceScanActivity(12254): ---->char permission:UNKNOW

04-21 18:28:25.475: E/DeviceScanActivity(12254): ---->char property:READ|WRITE|

04-21 18:28:25.475: E/DeviceScanActivity(12254): ---->char uuid:00002a04-0000-1000-8000-00805f9b34fb

04-21 18:28:25.475: E/DeviceScanActivity(12254): ---->char permission:UNKNOW

04-21 18:28:25.475: E/DeviceScanActivity(12254): ---->char property:READ

04-21 18:28:25.475: E/DeviceScanActivity(12254): -->service type:PRIMARY

04-21 18:28:25.475: E/DeviceScanActivity(12254): -->includedServices size:0

04-21 18:28:25.475: E/DeviceScanActivity(12254): -->service uuid:00001801-0000-1000-8000-00805f9b34fb

04-21 18:28:25.480: E/DeviceScanActivity(12254): ---->char uuid:00002a05-0000-1000-8000-00805f9b34fb

04-21 18:28:25.480: E/DeviceScanActivity(12254): ---->char permission:UNKNOW

04-21 18:28:25.480: E/DeviceScanActivity(12254): ---->char property:INDICATE

04-21 18:28:25.480: E/DeviceScanActivity(12254): -------->desc uuid:00002902-0000-1000-8000-00805f9b34fb

04-21 18:28:25.480: E/DeviceScanActivity(12254): -------->desc permission:UNKNOW

04-21 18:28:25.480: E/DeviceScanActivity(12254): -->service type:PRIMARY

04-21 18:28:25.480: E/DeviceScanActivity(12254): -->includedServices size:0

04-21 18:28:25.480: E/DeviceScanActivity(12254): -->service uuid:0000ffe0-0000-1000-8000-00805f9b34fb

04-21 18:28:25.480: E/DeviceScanActivity(12254): ---->char uuid:0000ffe1-0000-1000-8000-00805f9b34fb

04-21 18:28:25.480: E/DeviceScanActivity(12254): ---->char permission:UNKNOW

04-21 18:28:25.480: E/DeviceScanActivity(12254): ---->char property:READ|WRITE_NO_RESPONSE|NOTIFY|

04-21 18:28:25.490: E/DeviceScanActivity(12254): -------->desc uuid:00002902-0000-1000-8000-00805f9b34fb

04-21 18:28:25.490: E/DeviceScanActivity(12254): -------->desc permission:UNKNOW

04-21 18:28:25.490: E/DeviceScanActivity(12254): -------->desc uuid:00002901-0000-1000-8000-00805f9b34fb

04-21 18:28:25.490: E/DeviceScanActivity(12254): -------->desc permission:UNKNOW

04-21 18:28:26.025: E/DeviceScanActivity(12254): onCharRead BLE DEVICE read 0000ffe1-0000-1000-8000-00805f9b34fb -> 00

这里红字是由BluetoothGattCallback的onCharacteristicRead()回调而打出Log

以下Log是PC上的串口工具通过BLE模块发送过来,由BluetoothGattCallback的 onCharacteristicChanged()打出Log

04-21 18:30:18.260: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:18.745: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:19.085: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:19.350: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:19.605: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:19.835: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:20.055: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:20.320: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:20.510: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:20.735: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

04-21 18:30:21.000: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-0000-1000-8000-00805f9b34fb -> send data to phone

接下来贴出本文核心代码:

[java] view
plain
copyprint?

  1. public class DeviceScanActivity extends ListActivity {
  2. private final static String TAG = DeviceScanActivity.class.getSimpleName();
  3. private final static String UUID_KEY_DATA = "0000ffe1-0000-1000-8000-00805f9b34fb";
  4. private LeDeviceListAdapter mLeDeviceListAdapter;
  5. /**搜索BLE终端*/
  6. private BluetoothAdapter mBluetoothAdapter;
  7. /**读写BLE终端*/
  8. private BluetoothLeClass mBLE;
  9. private boolean mScanning;
  10. private Handler mHandler;
  11. // Stops scanning after 10 seconds.
  12. private static final long SCAN_PERIOD = 10000;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. getActionBar().setTitle(R.string.title_devices);
  17. mHandler = new Handler();
  18. // Use this check to determine whether BLE is supported on the device.  Then you can
  19. // selectively disable BLE-related features.
  20. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
  21. Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
  22. finish();
  23. }
  24. // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
  25. // BluetoothAdapter through BluetoothManager.
  26. final BluetoothManager bluetoothManager =
  27. (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  28. mBluetoothAdapter = bluetoothManager.getAdapter();
  29. // Checks if Bluetooth is supported on the device.
  30. if (mBluetoothAdapter == null) {
  31. Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
  32. finish();
  33. return;
  34. }
  35. //开启蓝牙
  36. mBluetoothAdapter.enable();
  37. mBLE = new BluetoothLeClass(this);
  38. if (!mBLE.initialize()) {
  39. Log.e(TAG, "Unable to initialize Bluetooth");
  40. finish();
  41. }
  42. //发现BLE终端的Service时回调
  43. mBLE.setOnServiceDiscoverListener(mOnServiceDiscover);
  44. //收到BLE终端数据交互的事件
  45. mBLE.setOnDataAvailableListener(mOnDataAvailable);
  46. }
  47. @Override
  48. protected void onResume() {
  49. super.onResume();
  50. // Initializes list view adapter.
  51. mLeDeviceListAdapter = new LeDeviceListAdapter(this);
  52. setListAdapter(mLeDeviceListAdapter);
  53. scanLeDevice(true);
  54. }
  55. @Override
  56. protected void onPause() {
  57. super.onPause();
  58. scanLeDevice(false);
  59. mLeDeviceListAdapter.clear();
  60. mBLE.disconnect();
  61. }
  62. @Override
  63. protected void onStop() {
  64. super.onStop();
  65. mBLE.close();
  66. }
  67. @Override
  68. protected void onListItemClick(ListView l, View v, int position, long id) {
  69. final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
  70. if (device == null) return;
  71. if (mScanning) {
  72. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  73. mScanning = false;
  74. }
  75. mBLE.connect(device.getAddress());
  76. }
  77. private void scanLeDevice(final boolean enable) {
  78. if (enable) {
  79. // Stops scanning after a pre-defined scan period.
  80. mHandler.postDelayed(new Runnable() {
  81. @Override
  82. public void run() {
  83. mScanning = false;
  84. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  85. invalidateOptionsMenu();
  86. }
  87. }, SCAN_PERIOD);
  88. mScanning = true;
  89. mBluetoothAdapter.startLeScan(mLeScanCallback);
  90. } else {
  91. mScanning = false;
  92. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  93. }
  94. invalidateOptionsMenu();
  95. }
  96. /**
  97. * 搜索到BLE终端服务的事件
  98. */
  99. private BluetoothLeClass.OnServiceDiscoverListener mOnServiceDiscover = new OnServiceDiscoverListener(){
  100. @Override
  101. public void onServiceDiscover(BluetoothGatt gatt) {
  102. displayGattServices(mBLE.getSupportedGattServices());
  103. }
  104. };
  105. /**
  106. * 收到BLE终端数据交互的事件
  107. */
  108. private BluetoothLeClass.OnDataAvailableListener mOnDataAvailable = new OnDataAvailableListener(){
  109. /**
  110. * BLE终端数据被读的事件
  111. */
  112. @Override
  113. public void onCharacteristicRead(BluetoothGatt gatt,
  114. BluetoothGattCharacteristic characteristic, int status) {
  115. if (status == BluetoothGatt.GATT_SUCCESS)
  116. Log.e(TAG,"onCharRead "+gatt.getDevice().getName()
  117. +" read "
  118. +characteristic.getUuid().toString()
  119. +" -> "
  120. +Utils.bytesToHexString(characteristic.getValue()));
  121. }
  122. /**
  123. * 收到BLE终端写入数据回调
  124. */
  125. @Override
  126. public void onCharacteristicWrite(BluetoothGatt gatt,
  127. BluetoothGattCharacteristic characteristic) {
  128. Log.e(TAG,"onCharWrite "+gatt.getDevice().getName()
  129. +" write "
  130. +characteristic.getUuid().toString()
  131. +" -> "
  132. +new String(characteristic.getValue()));
  133. }
  134. };
  135. // Device scan callback.
  136. private BluetoothAdapter.LeScanCallback mLeScanCallback =
  137. new BluetoothAdapter.LeScanCallback() {
  138. @Override
  139. public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
  140. runOnUiThread(new Runnable() {
  141. @Override
  142. public void run() {
  143. mLeDeviceListAdapter.addDevice(device);
  144. mLeDeviceListAdapter.notifyDataSetChanged();
  145. }
  146. });
  147. }
  148. };
  149. private void displayGattServices(List<BluetoothGattService> gattServices) {
  150. if (gattServices == null) return;
  151. for (BluetoothGattService gattService : gattServices) {
  152. //-----Service的字段信息-----//
  153. int type = gattService.getType();
  154. Log.e(TAG,"-->service type:"+Utils.getServiceType(type));
  155. Log.e(TAG,"-->includedServices size:"+gattService.getIncludedServices().size());
  156. Log.e(TAG,"-->service uuid:"+gattService.getUuid());
  157. //-----Characteristics的字段信息-----//
  158. List<BluetoothGattCharacteristic> gattCharacteristics =gattService.getCharacteristics();
  159. for (final BluetoothGattCharacteristic  gattCharacteristic: gattCharacteristics) {
  160. Log.e(TAG,"---->char uuid:"+gattCharacteristic.getUuid());
  161. int permission = gattCharacteristic.getPermissions();
  162. Log.e(TAG,"---->char permission:"+Utils.getCharPermission(permission));
  163. int property = gattCharacteristic.getProperties();
  164. Log.e(TAG,"---->char property:"+Utils.getCharPropertie(property));
  165. byte[] data = gattCharacteristic.getValue();
  166. if (data != null && data.length > 0) {
  167. Log.e(TAG,"---->char value:"+new String(data));
  168. }
  169. //UUID_KEY_DATA是可以跟蓝牙模块串口通信的Characteristic
  170. if(gattCharacteristic.getUuid().toString().equals(UUID_KEY_DATA)){
  171. //测试读取当前Characteristic数据,会触发mOnDataAvailable.onCharacteristicRead()
  172. mHandler.postDelayed(new Runnable() {
  173. @Override
  174. public void run() {
  175. mBLE.readCharacteristic(gattCharacteristic);
  176. }
  177. }, 500);
  178. //接受Characteristic被写的通知,收到蓝牙模块的数据后会触发mOnDataAvailable.onCharacteristicWrite()
  179. mBLE.setCharacteristicNotification(gattCharacteristic, true);
  180. //设置数据内容
  181. gattCharacteristic.setValue("send data->");
  182. //往蓝牙模块写入数据
  183. mBLE.writeCharacteristic(gattCharacteristic);
  184. }
  185. //-----Descriptors的字段信息-----//
  186. List<BluetoothGattDescriptor> gattDescriptors = gattCharacteristic.getDescriptors();
  187. for (BluetoothGattDescriptor gattDescriptor : gattDescriptors) {
  188. Log.e(TAG, "-------->desc uuid:" + gattDescriptor.getUuid());
  189. int descPermission = gattDescriptor.getPermissions();
  190. Log.e(TAG,"-------->desc permission:"+ Utils.getDescPermission(descPermission));
  191. byte[] desData = gattDescriptor.getValue();
  192. if (desData != null && desData.length > 0) {
  193. Log.e(TAG, "-------->desc value:"+ new String(desData));
  194. }
  195. }
  196. }
  197. }//
  198. }
  199. }

时间: 2024-08-05 11:57:08

Android 蓝牙4.0 BLE 理解的相关文章

Android 蓝牙4.0 BLE

Android ble (Bluetooth Low Energy) 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用. BLE是蓝牙4.0的核心Profile,主打功能是快速搜索,快速连接,超低功耗保持连接和传输数据,弱点是数据传输速率低,由于BLE的低功耗特点,因此普遍用于穿戴设备. 官方demo:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html 官方demo(

android蓝牙4.0(BLE)开发之ibeacon初步

此文使用的ibeacon模块是april beacon,至于什么是ibeacon.本文不做解释,具体请自查. 一个april beacon里携带的信息如下 0201061AFF4C0002159069BDB88C11416BAC3F33468C2788A3044B0378C60C09417072696C426561636F6E051250002003020A0000000000000000000000 具体是什么意思呢 02 Number of bytes that follow in firs

Android项目实战(三十四):蓝牙4.0 BLE 多设备连接

原文:Android项目实战(三十四):蓝牙4.0 BLE 多设备连接 最近项目有个需求,手机设备连接多个蓝牙4.0 设备 并获取这些设备的数据. 查询了很多资料终于实现,现进行总结. -------------------------------------------------------------------------------------------------------------------------------------------------------------

ym——物联网入口之一Android蓝牙4.0

如果还有同学不知道蓝牙4.0可以做什么请查看Android+蓝牙 4.0 将带来什么?,现在可以穿戴设备也大多用的是蓝牙4.0,如 智能体质秤,智能手环,智能血压计等等. 安卓4.3(API 18)为BLE的核心功能提供平台支持和API,App可以利用它来发现设备.查询服务和读写特性.相比传统的蓝牙,BLE更显著的特点是低功耗.这一优点使android App可以与具有低功耗要求的BLE设备通信,如近距离传感器.心脏速率监视器.健身设备等. 关键术语和概念 Generic Attribute P

Android蓝牙4.0之玩爆智能穿戴、家具(二)【进阶篇】

闲话中心 这几天最大的事可能就是美国总统的上任,双十一,还有乐视股价了,乍一看,好像和我们没什么关系,其实肯定是有的了,要不然他也成不了新闻啊,有一点我们得改变,就是我们必须要希望我们自己国家的企业能过强大,我们必须支持他们,哪怕他做的不够好,这个问题其实就像一个国家一样,我们都知道许多政策是不合理的,或者说有很多制度是坑人的,但是我们不能因为这些而不爱我们的国家,那么企业也是一样,就拿乐视来说,股价跌了,公司遇到资金问题了,你看看这些媒体都在报道什么,全是负面消息,马上倒闭了,或者说是撑不住了

蓝牙4.0 BLE

读了N多文档,其中推荐的有: Webee的<蓝牙4.0是战演练> Ghostyu的 <BLE权威教程> 1:透穿实现: 利用TI的BLE包里的工程直接烧 上位设备用 central,下位设备用peripheral工程 做以下处理: central 的NPI初始化时添加uart CB,并在串口回调函数中 直接添加write char函数写进特征值(实现上位从串口接收并通过蓝牙发送), 使能特征值通知,并在通知处理事件中将数据从串口发送(实现上位的从蓝牙接受并从串口发送) periph

android 蓝牙4.0 开发介绍

最近一直在研究一个蓝牙功能 由于本人是菜鸟  学起来比较忙 一直搞了好久才弄懂 , 网上对蓝牙4.0也就是几个个dome 抄来抄去,全是英文注解 , 对英语不好的朋友来说 真是硬伤 , 一些没必要的描述罗里吧嗦 , 关键的方法接口 一笔带过 .........算了不吐槽了.我就介绍一下我最近的学习心得吧 ,简单的什么开启  蓝牙 搜索蓝牙什么的我就不说了 你们百度一下 android 蓝牙 4.0 一大坨.看完再来看我的博客也行  ,我就介绍点 网上那些 一笔带过 比较重要的接口回调 之类的.

iOS开发 之 可穿戴设备 蓝牙4.0 BLE 开发

1 前言 当前有越来越多的可穿戴设备使用了蓝牙4.0 BLE(Bluetooth Low Energy).对于iOS开发而言,Apple之前专门推出CoreBluetooth的Framework来支持BLE的开发.对于硬件开发有了解的朋友应该知道,在之前使用低版本的蓝牙的设备,要连接到iOS设备上,需要注册MFI,拥有MFI协议才能进行相应的开发.如果大家关注我之前对LEGO EV3的研究,就可以发现,EV3是使用了蓝牙2.1,因此需要MFI协议来进行开发. 本文将一步一步讲解如何使用CoreB

蓝牙4.0 BLE学习笔记

一.知识普及 1.蓝牙4.0分为两个部分: 1)Bluetooth Ready,兼容传统蓝牙的高速部分: 2)Bluetooth Smart,BLE(Bluetooth Low Energy),功耗低,速率低.最大传输速率4~5k字节/s: 2.BLE协议栈: 1)只是一个协议规范,BLE协议栈是该协议的代码实现:蓝牙组织SIG负责制定协议,芯片公司负责实现协议: 2)BLE协议栈是芯片公司预先编好的源码或者库: 3.CC2540/2541,CC254x就是一颗带有蓝牙功能的51单片机,BLE协