当前位置: 首页 > news >正文

网站导航菜单兰php是专门做网站的

网站导航菜单兰,php是专门做网站的,公共资源交易中心官网首页,工地模板1 前言 当前有越来越多的可穿戴设备使用了蓝牙4.0 BLE#xff08;Bluetooth Low Energy)。对于iOS开发而言#xff0c;Apple之前专门推出CoreBluetooth的Framework来支持BLE的开发。对于硬件开发有了解的朋友应该知道#xff0c;在之前使用低版本的蓝牙的设备#xff0c;要…1 前言 当前有越来越多的可穿戴设备使用了蓝牙4.0 BLEBluetooth Low Energy)。对于iOS开发而言Apple之前专门推出CoreBluetooth的Framework来支持BLE的开发。对于硬件开发有了解的朋友应该知道在之前使用低版本的蓝牙的设备要连接到iOS设备上需要注册MFI拥有MFI协议才能进行相应的开发。如果大家关注我之前对LEGO EV3的研究就可以发现EV3是使用了蓝牙2.1因此需要MFI协议来进行开发。 本文将一步一步讲解如何使用CoreBluetooth框架来与各种可穿戴设备进行通信使用 小米手环 来进行基本的测试。  2 开发环境 1 Macbook Pro Mac OS X 10.10 2 Xcode 6.3.2 3 iPhone 5s v8.1 4 小米手环 3 基本流程 要开发蓝牙需要对整个通讯过程有个基本了解。这里我摘录一些Apple官方的文档Core Bluetooth Programming Guide的图片来加以说明。这个文档其实对于开发的流程写的是非常的清楚大家最好可以看一下。 3.1 可穿戴设备与iOS互联方式 从上面这幅图可以看到我们的iOS设备是Central用来接收数据和发送命令而外设比如小米手环是Peripheral向外传输数据和接收命令。我们要做的就是通过Central来连接Peripheral然后实现数据的接收和控制指令的发送。在做到这一步之后再根据具体的硬件对接收到的数据进行parse解析。 3.2 可穿戴设备蓝牙的数据结构 这里用的是心率设备来做说明每个外设Peripheral都有对应的服务Service比如这里是心率Service。一个外设可以有不止一个s、Service。每个service里面可以有多个属性Characteristic比如这里有两个Characteristic一个是用来测量心率一个是用来定位位置。 那么很关键的一点是每个Service每个Characteristic都是用UUID来确定的。UUID就是每个Service或Characteristic的identifier。 大家可以在iPhone上下载LightBlue这个应用。可以在这里查看一些设备的UUID。  在实际使用中我们都是要通过UUID来获取数据。这点非常重要。 在CoreBluetooth中其具体的数据结构图如下  4 Step-By-Step 上手BLE开发 4.1 Step 1 创建CBCentralManager 从名字上大家可以很清楚的知道这个类是用来管理BLE的。我们也就是通过这个类来实现连接。 先创建一个 property (nonatomic,strong) CBCentralManager *centralManager;dispatch_queue_t centralQueue dispatch_queue_create(com.manmanlai, DISPATCH_QUEUE_SERIAL); self.centralManager [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue]; 12341234然后关键在于CBCentralManagerDelegate的使用。这个之后再讲。 4.2 Step 2 寻找CBPeripheral外设 有了CBCentralManager接下来就是寻找CBPeripheral外设方法很简单 [self.centralManager scanForPeripheralsWithServices:[] options:nil]; 11这里的Service就是对应的UUID如果为空这scan所有service。 4.3 Step 3 连接CBPeripheral 在上一步中如果找到了设备则CBCentralManager的delegate会调用下面的方法 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {NSLog(name:%,peripheral); if (!peripheral || !peripheral.name || ([peripheral.name isEqualToString:])) { return; } if (!self.peripheral || (self.peripheral.state CBPeripheralStateDisconnected)) { self.peripheral peripheral; self.peripheral.delegate self; NSLog(connect peripheral); [self.centralManager connectPeripheral:peripheral options:nil]; } } 123456789101112131415123456789101112131415我们在这里创建了一个CBPeripheral的对象然后直接连接 CBPeripheral的对象也需要设置delegate. 4.4 Step 4 寻找Service 如果Peripheral连接成功的话就会调用delegate的方法 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {if (!peripheral) {return;}[self.centralManager stopScan]; NSLog(peripheral did connect); [self.peripheral discoverServices:nil]; } 12345678910111213141234567891011121314我们这里先停止Scan然后让Peripheral外设寻找其Service。 4.5 Step 5 寻找Characteristic 找到Service后会调用下面的方法 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {NSArray *services nil;if (peripheral ! self.peripheral) { NSLog(Wrong Peripheral.\n); return ; } if (error ! nil) { NSLog(Error %\n, error); return ; } services [peripheral services]; if (!services || ![services count]) { NSLog(No Services); return ; } for (CBService *service in services) { NSLog(service:%,service.UUID); [peripheral discoverCharacteristics:nil forService:service]; } } 1234567891011121314151617181920212223242526272812345678910111213141516171819202122232425262728我们根据找到的service寻找其对应的Characteristic。 4.6 Step 6 找到Characteristic后读取数据 找到Characteristic后会调用下面的delegate方法 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {NSLog(characteristics:%,[service characteristics]);NSArray *characteristics [service characteristics]; if (peripheral ! self.peripheral) { NSLog(Wrong Peripheral.\n); return ; } if (error ! nil) { NSLog(Error %\n, error); return ; } self.characteristic [characteristics firstObject]; //[self.peripheral readValueForCharacteristic:self.characteristic]; [self.peripheral setNotifyValue:YES forCharacteristic:self.characteristic]; 123456789101112131415161718123456789101112131415161718这里我们可以使用readValueForCharacteristic:来读取数据。如果数据是不断更新的则可以使用setNotifyValue:forCharacteristic:来实现只要有新数据就获取。 4.7 Step 7 处理数据 读到数据后会调用delegate方法 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {NSData *data characteristic.value;// Parse data ...}123456712345674.8 Step 8 向设备写数据 这个很简单只要使用 [self.peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse]; 11data是NSData类型。 5 实验 使用小米手环实验得到如下结果 2015-06-10 16:52:31.607 KetherDemo[13786:1792995] scaning device 2015-06-10 16:52:33.474 KetherDemo[13786:1793032] name:CBPeripheral: 0x1700e4380, identifier 6FF833E3-93C1-28C6-CBC0-74A706AAAE31, name LS_SCA16, state disconnected 2015-06-10 16:52:33.475 KetherDemo[13786:1793032] connect peripheral 2015-06-10 16:52:37.538 KetherDemo[13786:1793031] peripheral did connect 2015-06-10 16:52:37.984 KetherDemo[13786:1793031] service:FEE7 2015-06-10 16:52:37.985 KetherDemo[13786:1793031] service:Device Information 2015-06-10 16:52:38.099 KetherDemo[13786:1793032] characteristics:( CBCharacteristic: 0x17409c250, UUID FEC8, properties 0x20, value (null), notifying NO, CBCharacteristic: 0x17409c200, UUID FEC7, properties 0x8, value (null), notifying NO ) 2015-06-10 16:52:38.100 KetherDemo[13786:1793032] Kether did connect 2015-06-10 16:52:38.101 KetherDemo[13786:1793032] Kether did connect 2015-06-10 16:52:38.280 KetherDemo[13786:1793031] characteristics:( CBCharacteristic: 0x17009f270, UUID Manufacturer Name String, properties 0x2, value (null), notifying NO, CBCharacteristic: 0x17009f2c0, UUID Model Number String, properties 0x2, value (null), notifying NO, CBCharacteristic: 0x17009f310, UUID Serial Number String, properties 0x2, value (null), notifying NO, CBCharacteristic: 0x17009eb90, UUID Hardware Revision String, properties 0x2, value (null), notifying NO, CBCharacteristic: 0x17009f0e0, UUID Firmware Revision String, properties 0x2, value (null), notifyi NO, 1234567891011121314151617181920211234567891011121314151617181920216 小结 通过上面的方法我们就可以轻松的对BLE进行开发。实际上比想象的要简单。转载于:https://www.cnblogs.com/liaolijun/p/6689858.html
http://www.zqtcl.cn/news/584865/

相关文章:

  • 建设银行观澜支行网站做网站公司汉狮网络
  • 荆州学校网站建设seo专业培训机构
  • 网站制作上网建站程序的价钱
  • 阿里巴巴网站建设规划24小时学会网站建设pdf
  • wordpress建站以后网络公司注册资金多少
  • wordpress下载站模板优秀网站开发公司
  • ppt模板免费下载完整版免费网站微网站开发商
  • 网站建设前的分析第一小节内容wordpress自带主题下载失败
  • 深圳微信网站设计网站建设设计制作外包
  • 做数模必逛的网站wordpress 培训 主题
  • 开发网站语言天元建设集团有限公司电话
  • 兼职做网站访问量和数据关于外贸公司的网站模板
  • 旅游网站设计与实现软件定制报价单
  • 上海专业网站建站公网站开发人员
  • 淄博网站建设公司有几家网络培训平台下载
  • 优秀网站展示仲恺做网站
  • 达州做淘宝网站制作3d动画的软件
  • 服务器主机搭建网站wordpress 隐私设置
  • 专业的网站制作公司wordpress 网页模板
  • 中国建设人才服务信息网是什么网站深圳居家办公最新通知
  • 中国建设银行网站招聘wordpress 文件下载插件
  • 福州企业建站程序莆田市的网站建设公司
  • 龙岩做网站推广龙华品牌网站建设
  • 如何用网站开发工具停止网页进程微信网站 教程
  • 高端网站网站设计网站建设怎么分析市场分析
  • 株洲网站设计外包运营wordpress seo插件教程
  • 做湘菜的网站wordpress外贸网站
  • 可以做书的网站做网站的软件叫什么
  • 深圳营销型网站公司电话网站优化北京如何联系?
  • 网站配资公司网站织梦怎么关闭网站