蓝牙-b
最近智能家居比较火,好多公司开始开发通过蓝牙对智能家居进行连接控制!下面,我就把自己总结的蓝牙方面的知识分享一下!求吐槽!!!!O(∩_∩)O。。。
1.导入头文件#import <CoreBluetooth/CoreBluetooth.h>
2.设置中心及外设的属性
@property(nonatomic,strong)CBCentralManager*cbCentralMgr;//中心(发起连接)
@property(nonatomic,strong)CBPeripheral*myPeripheral;//外部设备(被动连接)
3.继承代理方法<CBCentralManagerDelegate,CBPeripheralDelegate>
4.创建中心设备的实例并设置代理
self.cbCentralMgr= [[CBCentralManageralloc]initWithDelegate:selfqueue:nil];
5.中心设备设置delegate后会自动调用本机蓝牙状态的方法
- (void)centralManagerDidUpdateState:(CBCentralManager*)central
{
switch(central.state)
{
caseCBCentralManagerStateUnsupported:
NSLog(@"The platform/hardware doesn't support Bluetooth Low Energy.");
break;
caseCBCentralManagerStateUnauthorized:
NSLog(@"The app is not authorized to use Bluetooth Low Energy.");
break;
caseCBCentralManagerStatePoweredOff:
NSLog(@"Bluetooth is currently powered off.");
break;
caseCBCentralManagerStatePoweredOn:
{
NSLog(@"CBCentralManagerStatePoweredOn");
}
break;
caseCBCentralManagerStateUnknown:
default:
break;
}
}
6.在状态为CBCentralManagerStatePoweredOn的时候可以通过中心设备扫描周边的设备
[self.cbCentralMgr scanForPeripheralsWithServices:niloptions:@{CBCentralManagerScanOptionAllowDuplicatesKey: [NSNumbernumberWithBool:YES]}];
7.一旦扫描到设备就会自动调用代理方法
//发现蓝牙设备,可能发现不止一个蓝牙设备,所以该方法可能被调用多次
- (void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber*)RSSI{}
8.在扫描到一个设备时,我们可以进行外部设备和中心的连接
//连接某个蓝牙,一个中心设备可以连接多个周围的蓝牙设备,苹果最多连接10个外设
[self.cbCentralMgr connectPeripheral:peripheral options:[NSDictionary dictionaryWithObject:[NSNumbernumberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
9.在连接的时候同时可以关闭中心的扫描
[self.cbCentralMgrstopScan];
10.在中心和外部设备的连接过程中,可能会调用以下几个方法:
a.当连接成功时调用
//当连接上某个蓝牙之后,CBCentralManager会通知代理处理
- (void)centralManager:(CBCentralManager*)central didConnectPeripheral:(CBPeripheral*)peripheral{}
b.当连接失败时调用
//当连接蓝牙失败的时候会调用
- (void)centralManager:(CBCentralManager*)central didFailToConnectPeripheral:(CBPeripheral*)peripheral error:(NSError*)error{}
c.当连接断开时调用
//当中心主动与外设断开或外设主动和中心断开成功时调用
- (void)centralManager:(CBCentralManager*)central didDisconnectPeripheral:(CBPeripheral*)peripheral error:(nullableNSError*)error{}
11.中心主动断开与外设的连接
[self.cbCentralMgrcancelPeripheralConnection:peripheral];
12.连接成功后,需要设置外设代理并且查询所有蓝牙服务或蓝牙某一个服务
//因为在后面我们要从外设蓝牙那边再获取一些信息并与之通讯,这些过程会有一些事件可能要处理,所以要给这个外设设置代理
peripheral.delegate=self;
//查询所有蓝牙服务,或查询蓝牙外设上的指定服务
[self.myperipheraldiscoverServices:nil];
13.查询所有服务时会调用外设的方法
//返回的蓝牙服务通知通过代理实现
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverServices:(NSError*)error
{
for(CBService* serviceinperipheral.services){
[peripheraldiscoverCharacteristics:nilforService:service];
}
}
14.在这个方法里会进行扫描所有特征值,进而调用外设查询特征值的代理方法
//返回的蓝牙特征值通知通过代理实现
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverCharacteristicsForService:(CBService*)service error:(NSError*)error{
}
15.在上面的方法中可以获取到已经与中心连接的外设内有哪些特征值,并且可以通过打开通知来读取特征值内数据的变化
[peripheralsetNotifyValue:YESforCharacteristic:characterstic];
16.特征值内数据变化时会调用外设的代理方法
- (void) peripheral:(CBPeripheral*)aPeripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error{}
17.在该方法里面读取的特征值内容是NSData类型,我们也可以转换为字节数组进行判断
NSData*data = characteristic.value;
//data转byte数组
Byte*testByte = (Byte*)[databytes];
for(inti=0;i<[datalength]; i++){
if((testByte[1] ==2)&&(testByte[0] ==2)) {
//做简单的演示判断
}
}
18.与此同时,我们也可以向某一个特征发送信息,只需要调用下面的方法
//根据蓝牙对象和特性发送数据
-(void)sendDatawithperipheral:(CBPeripheral*)peripheral characteristic:(NSString*)characteristicStr service:(NSString*) service data:(NSData*)data {
for(int i=0; i<service.characteristics; i++)
if([peripheral.services[i].UUIDisEqual:[CBUUIDUUIDWithString:service]]) {
for(CBCharacteristic*characteristicin[[peripheral.servicesobjectAtIndex:i]characteristics]){
//找到通信的的特性
if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:characteristicStr]]){
[peripheralwriteValue:dataforCharacteristic:characteristictype:CBCharacteristicWriteWithoutResponse];
}
}
}
}
}
19.那么,我们该如何读取中心和外部设备的信号值?信号值越小说明信号越弱
a.当未连接时,我们可以通过扫描外设获取- (void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber*)RSSI{}方法里的RSSI值
b.当连接时,我们可以通过 [peripheralreadRSSI]来调用 - (void)peripheral:(CBPeripheral*)peripheral didReadRSSI:(NSNumber*)RSSI error:(NSError*)error{}方法获取RSSI值
原文链接:http://www.jianshu.com/p/1461ebb498ab
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
蓝牙-b的更多相关文章
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总
--系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...
- 2、利用蓝牙定位及姿态识别实现一个智能篮球场套件(二)——CC2540/CC2541基于广播的RSSI获得
CC2541一拖多例程中RSSI获得是通过一个事件回调函数实现的,前提是需要连接上蓝牙设备. 这个对于多点定位来说是不可行的,由于主机搜索蓝牙设备过程中也能获得当前蓝牙设备的RSSI等信息,因此可基于 ...
- 1、利用蓝牙定位及姿态识别实现一个智能篮球场套件(一)——用重写CC2541透传模块做成智能手环
一.预言 要实现一个智能篮球场套件,需要设计一个佩戴在篮球运动员手臂上的可以检测投篮.记步的手环,以及一套可以根据RSSI定位运动员的蓝牙定位装置.下面是大致需要的步骤: 首先,需要用CC2541透传 ...
- 蓝牙Bluetooth技术小知识
蓝牙Bluetooth技术以及广泛的应用于各种设备,并将继续在物联网IoT领域担任重要角色.下面搜集整理了一些关于蓝牙技术的小知识,以备参考. 蓝牙Bluetooth技术始创于1994年,其名字来源于 ...
- 蓝牙协议中的SBC编码
一.从信息的传输说起  上图是一个典型的蓝牙耳机应用场景.手机上的音频信息经过编码以后通过蓝牙协议被蓝牙耳机接收,经过解码以后,蓝牙耳机成功获取手机上的音频信息,然后再转化为振动被人耳识别.这是一个 ...
- 蓝牙4.0(BLE)开发
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/26740237 作者:小马 IOS学习也一段时间了,该上点干货了.前段时间研究了一下 ...
- BluetoothChat用于蓝牙串口通信的修改方法
本人最近在研究嵌入式的串口通信,任务是要写一个手机端的遥控器用来遥控双轮平衡小车.界面只用了一个小时就写好了,重要的问题是如何与板子所带的SPP-CA蓝牙模块进行通信. SPP-CA模块自带代码,在这 ...
- (转)android 蓝牙通信编程
转自:http://blog.csdn.net/pwei007/article/details/6015907 Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输. 本文档描述了怎样 ...
- 蓝牙协议分析(7)_BLE连接有关的技术分析
转自:http://www.wowotech.net/bluetooth/ble_connection.html#comments 1. 前言 了解蓝牙的人都知道,在经典蓝牙中,保持连接(Connec ...
- Android端简易蓝牙聊天通讯App(原创)
欢迎转载,但请注明出处!谢谢.http://www.cnblogs.com/weizhxa/p/5792775.html 最近公司在做一个蓝牙串口通讯的App,有一个固定的蓝牙设备,需要实现手机连接相 ...
随机推荐
- C++基础复习
1.Object-C也是面向对象的语言:2.#include<iostream> //#include是一个预处理指令3.using namespace std; //std是命名空间,u ...
- css分割线 文字居中的7种实现方式
最近开始研究前端,会不定期更新一些小块代码,写下自己的学习笔记,也希望能和大家一起进步! 1.单个标签实现分隔线: <html> <head lang="en"& ...
- [Ruby] Ruby Variable Scope
Scope defines where in a program a variable is accessible. Ruby has four types of variable scope, lo ...
- Android(java)学习笔记226:服务(service)之为什么使用服务
1.服务 service 长期在后台运行的进程,一般没有应用程序界面 2.进程线程和应用程序之间的关系 应用程序开启,系统启动一个Linux进程,所有的组件都是运行在同一个进程的同一个线程(mai ...
- 模板-->Matrix重载运算符:+,-,x
如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 poj_2118_Firepersons,my_ac_code 简单的测试 INPUT: 1 2 3 1 3 4 3 -1 ...
- 剪切板 复制文本 ClipboardManager
代码 public class MainActivity extends ListActivity { private EditText tv_info; private Clipbo ...
- 洛谷 1503 鬼子进村 (set)
/*set加速维护*/ #include<iostream> #include<cstdio> #include<cstring> #include<set& ...
- <html:form>、 <html:text>、<html:password>、<html:submit> 标签
用Struts标签来写表单元件, 引用: <%@ taglib uri="/tags/struts-html" prefix="html" %> 例 ...
- c# 学习笔记(二)
c#3.0 新特性 扩展方法 扩展方法允许编写和声明它的类之外的关联类的方法 用于没有源代码或者类是密封的,需要给类扩展新方法时 1.扩展方法必须被声明为static2.扩展方法声明所在的类必须被声 ...
- 浏览器中 for in 反射 对象成员 的差异
http://www.cnblogs.com/_franky/archive/2010/05/08/1730437.html 下面是例子 function test(url, obj) { if($( ...