iOS开发之--蓝牙开发实战

转载自:http://www.cnblogs.com/zyjzyj/p/6029968.html ,感谢英杰

前言

最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文章实在太多了,成吨成吨的文章出现,但是很遗憾都只是一些皮毛,或者只是简单的介绍一下基本概念而已,对于一些小白可能还有很多很多疑惑,所以萌生了写一篇文章,并附上实际例子的demo,供即将项目中准备开发的伙伴参考。

正文

首先

我们得明确一下很重要的几个概念
1.当前ios中开发蓝牙所运用的系统库是<CoreBluetooth/CoreBluetooth.h>
2.蓝牙外设必须为4.0及以上,否则无法开发,蓝牙4.0设备因为低耗电,所以也叫做BLE。
3.CoreBluetooth框架的核心其实是两个东西,peripheral和central, 可以理解成外设和中心,就是你的苹果手机就是中心,外部蓝牙称为外设。
4.服务和特征(service and characteristic):简而言之,外部蓝牙中它有若干个服务service(服务你可以理解为蓝牙所拥有的能力),而每个服务service下拥有若干个特征characteristic(特征你可以理解为解释这个服务的属性)。
5.Descriptor(描述)用来描述characteristic变量的属性。例如,一个descriptor可以规定一个可读的描述,或者一个characteristic变量可接受的范围,或者一个characteristic变量特定的单位。
6.跟硬件亲测,Ios蓝牙每次最多接收155字节的数据,安卓5.0以下最大接收20字节,5.0以上可以更改最大接收量,能达到500多字节。

其次

通过以上关键信息的解释,然后看一下蓝牙的开发流程:

  1. 建立中心管理者
  2. 扫描外设(discover)
  3. 连接外设(connect)
  4. 扫描外设中的服务和特征(discover)
    4.1 获取外设的services
    4.2 获取外设的Characteristics,获取Characteristics的值,
    获取Characteristics的Descriptor和Descriptor的值
  5. 与外设做数据交互(explore and interact)
  6. 断开连接(disconnect)

具体代码

1.创建一个中心管理者

//只要一触发这句代码系统会自动检测手机蓝牙状态,你必须实现其代理方法,当然得添加<CBCentralManagerDelegate>
CBCentralManager *theManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];

//从这个代理方法中你可以看到所有的状态,其实我们需要的只有on和off连个状态
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
        switch (central.state) {
            case CBCentralManagerStateUnknown:
                NSLog(@">>>CBCentralManagerStateUnknown");
                break;
            case CBCentralManagerStateResetting:
                NSLog(@">>>CBCentralManagerStateResetting");
                break;
            case CBCentralManagerStateUnsupported:
                NSLog(@">>>CBCentralManagerStateUnsupported");
                break;
            case CBCentralManagerStateUnauthorized:
                NSLog(@">>>CBCentralManagerStateUnauthorized");
                break;
            case CBCentralManagerStatePoweredOff:
                NSLog(@">>>CBCentralManagerStatePoweredOff");
                break;
             case CBCentralManagerStatePoweredOn:
                NSLog(@">>>CBCentralManagerStatePoweredOn");
                break;   
            default:
                break;
        }

当发现蓝牙状态是开启状态,你就可以利用中央设备进行扫描外设,如果为关闭状态,系统会自动弹出让用户去设置蓝牙,这个不需要我们开发者关心。

2.利用中心去扫描外设

//两个参数为nil,默认扫描所有的外设,可以设置一些服务,进行过滤搜索
 [theManager scanForPeripheralsWithServices:nil options:nil];

2.1当扫描到外设,触发以下代理方法

在这里需要说明的是,
一.当扫描到外设,我们可以读到相应外设广播信息,RSSI信号强度(可以利用RSSI计算中心和外设的距离)。
二.我们可以根据一定的规则进行连接,一般是默认名字或者名字和信号强度的规则来连接。
三.像我现在做的无钥匙启动车辆锁定车辆,就需要加密通讯,不能谁来连接都可以操作车辆,需要和外设进行加密通讯,但是一切的通过算法的校验都是在和外设连接上的基础上进行,例如连接上了,你发送一种和硬件约定好的算法数据,硬件接收到校验通过了就正常操作,无法通过则由硬件(外设)主动断开。

//这里默认扫到MI,主动连接,当然也可以手动触发连接
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
    NSLog(@"扫描连接外设:%@ %@",peripheral.name,RSSI);

    if ([peripheral.name hasSuffix:@"MI"]) {
        //保存外设,并停止扫描,达到节电效果
        thePerpher = peripheral;
        [central stopScan];
       //进行连接
        [central connectPeripheral:peripheral options:nil];
    }

}

3.当连接到外设,会调用以下代理方法

这里需要说明的是
当成功连接到外设,需要设置外设的代理,为了扫描服务调用相应代理方法

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{

    NSLog(@"连接外设成功!%@",peripheral.name);
    [peripheral setDelegate:self];
    [peripheral discoverServices:nil];
}

//连接外设失败
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"连接到外设 失败!%@ %@",[peripheral name],[error localizedDescription]);
}

4.扫描外设中的服务和特征

//扫描到服务
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
    if (error)
    {
        NSLog(@"扫描外设服务出错:%@-> %@", peripheral.name, [error localizedDescription]);
        return;
    }
    NSLog(@"扫描到外设服务:%@ -> %@",peripheral.name,peripheral.services);
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:nil forService:service];
    }
    NSLog(@"开始扫描外设服务的特征 %@...",peripheral.name);

}
//扫描到特征
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
    if (error)
    {
        NSLog(@"扫描外设的特征失败!%@->%@-> %@",peripheral.name,service.UUID, [error localizedDescription]);
        return;
    }

    NSLog(@"扫描到外设服务特征有:%@->%@->%@",peripheral.name,service.UUID,service.characteristics);
    //获取Characteristic的值
    for (CBCharacteristic *characteristic in service.characteristics){

  //这里外设需要订阅特征的通知,否则无法收到外设发送过来的数据
  [peripheral setNotifyValue:YES forCharacteristic:characteristic];

//这里以小米手环为例,当我们定义好每个特征是干什么用的,我们需要读取这个特征的值,当特征值更新了会调用
//- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error方法
//需要说明的是UUID是硬件定义好给你,如果硬件也是个新手,那你可以先打印出所有的UUID,找出有用的
            //步数
            if ([characteristic.UUID.UUIDString isEqualToString:@"FF06"])
            {
                [peripheral readValueForCharacteristic:characteristic];
            }

            //电池电量
            else if ([characteristic.UUID.UUIDString isEqualToString:@"FF0C"])
            {
                [peripheral readValueForCharacteristic:characteristic];
            }

            else if ([characteristic.UUID.UUIDString isEqualToString:@"2A06"])
            {
                //震动
                theSakeCC = characteristic;
            }

        }
    }


}

//扫描到具体的值->通讯主要的获取数据的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error
{
    if (error) {
        NSLog(@"扫描外设的特征失败!%@-> %@",peripheral.name, [error localizedDescription]);
        return;
    }
    NSLog(@"%@ %@",characteristic.UUID.UUIDString,characteristic.value);
    if ([characteristic.UUID.UUIDString isEqualToString:@"FF06"]) {
        Byte *steBytes = (Byte *)characteristic.value.bytes;  
        int steps = bytesValueToInt(steBytes);

    }
    else if ([characteristic.UUID.UUIDString isEqualToString: @"FF0C"])
    {
        Byte *bufferBytes = (Byte *)characteristic.value.bytes;
        int buterys = bytesValueToInt(bufferBytes)&0xff;
        NSLog(@"电池:%d%%",buterys);

    }
    else if ([characteristic.UUID.UUIDString isEqualToString:@"2A06"])
    {
        Byte *infoByts = (Byte *)characteristic.value.bytes;
        //这里解析infoByts得到设备信息

    }

}

5.与外设做数据交互

需要说明的是苹果官方提供发送数据的方法很简单,只需要调用下面的方法

- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type

我们只需要在搜索每个服务的特征,记录这个特征,然后向这个特征发送数据就可以了。

6.断开连接

调用以下代码,需要说明的是中心断开与外设的连接。

- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

以上呢是整个蓝牙的开发过程,系统提供的框架api就这么多,开发起来也不是很难,要是你认为这篇文章到这里就结束了,你就大错特错了,这篇文章的精华内容将从这里开始,由于公司项目的保密性,我不能以它为例,那我就以小米手环为实例,主要分享一下数据解析。

精华部分

1.当调用了以下代理方法的时候,我们需要处理接收到的数据

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error

小米手环所定义的几个UUID如下:

@"FF06" 这个UUID定义的是步数
@"FF0C" 这个UUID定义的是电量
@"2A06"这个UUID定义的是震动
@"FF01"这个UUID定义的是相关的设备信息

通过以上的UUID,我们可以读取到步数,电量,操作手环震动,并读取手环相应设备的信息,这里需要说明的是我并不清楚设备信息的具体协议,所以这里没法解析。

if ([characteristic.UUID.UUIDString isEqualToString:STEP]) {
        Byte *steBytes = (Byte *)characteristic.value.bytes;  
        int steps = bytesValueToInt(steBytes);
        NSLog(@"步数:%d",steps);
    }

当我们读到步数这个UUID时,我们可以拿到value,小米手环所定义的协议是4个字节,我们需要将4个字节转换为int 类型即可
方法如下

//4个字节Bytes 转 int
unsigned int  bytesValueToInt(Byte *bytesValue) {

 unsigned int  intV;
    intV = (unsigned int ) ( ((bytesValue[3] & 0xff)<<24)
                            |((bytesValue[2] & 0xff)<<16)
                            |((bytesValue[1] & 0xff)<<8)
                            |(bytesValue[0] & 0xff));
    return intV;
}

需要说明的是这个方法是C语言的方法,采用位与运算,当然如果项目中需要另一种方式的转换,如:发过来两字节需要你转换为int,如果你不会转换,可以去网上搜索,我会在文章后附一些常用的转换方法。
这里重点说明的是步数读取,剩余类似。

2.当我们给外设发送数据时,我们需要跟硬件定协议,当然这是在开始项目之前做好的事情。

小米手环协议中震动命令的触发,是向硬件发送一个10进制的 2
这里需要说明的是我们发送数据给硬件一般是字节数组,然后将他转换为NSData发送。

      //这里为了严谨起见,需要判断外设和特征是否存在,如果存在发送数据
       if (thePerpher && theSakeCC) {
        Byte zd[1] = {2};
        NSData *theData = [NSData dataWithBytes:zd length:1];
        [thePerpher writeValue:theData forCharacteristic:theSakeCC type:CBCharacteristicWriteWithoutResponse];

    }

这里需要再添加一点,如果协议要求你发ASCII码,例如‘SHAKE’,只需要这么处理

 if (thePerpher && theSakeCC) {
        Byte zd[] = {'S','H','A','K','E'};
        NSData *theData = [NSData dataWithBytes:zd length:1];
        [thePerpher writeValue:theData forCharacteristic:theSakeCC type:CBCharacteristicWriteWithoutResponse];

    }

3.项目中实际开发的运用
当我们面对实际开发时,我们不可能这么直接去在一个控制器中去写这么多代码,如果你说这没多少啊,那我无话可说了😄。。。当然有人会说运用三方库的啊,babyBluetooth在github上star还是挺高的,我的观点是没有必要去依赖所谓的三方库,有的三方库高度封装性会致使我们如果遇到错误时无法排查,所以三方库慎用,当然你可以参考一些Star很高的三方库,看大神的代码思想,有利于自己读写代码的能力。

我的主要思路是封装一个单例类,封装好扫描的方法,读取数据的方法(一般是代理回调),发送指令(例如小米的震动)方法,而在读取数据中我们可以采用模型的思想,当收到蓝牙外设发送过来的数据时候,我们解析完后包装成模型通过代理回传过去,以后我们在控制器中每次拿到的都是模型数据,这样处理起来方便大大的。
下面将小米手环demo附上,供需要的朋友参考学习,如果文章中我有什么没有说的很明白,或者什么疑惑可以留言。

demo https://github.com/markdashi/MIBLE

附常用转换方法

@interface NSString (Extension)

//16进制字符串转成int
- (int)convertHexStringToINT;

//16进制字符串转换为2进制的字符串
- (NSString *)getBinaryByhex;
@end

@implementation NSString (Extension)
//不考虑内存溢出
- (int)convertHexStringToINT
{
    UInt64 mac1 =  strtoul([self UTF8String], 0, 16);

    return [[NSString stringWithFormat:@"%llu",mac1] intValue];
}

- (NSString *)getBinaryByhex
{
    NSMutableDictionary  *hexDic = [[NSMutableDictionary alloc] init];

    hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];

    [hexDic setObject:@"0000" forKey:@"0"];

    [hexDic setObject:@"0001" forKey:@"1"];

    [hexDic setObject:@"0010" forKey:@"2"];

    [hexDic setObject:@"0011" forKey:@"3"];

    [hexDic setObject:@"0100" forKey:@"4"];

    [hexDic setObject:@"0101" forKey:@"5"];

    [hexDic setObject:@"0110" forKey:@"6"];

    [hexDic setObject:@"0111" forKey:@"7"];

    [hexDic setObject:@"1000" forKey:@"8"];

    [hexDic setObject:@"1001" forKey:@"9"];

    [hexDic setObject:@"1010" forKey:@"A"];

    [hexDic setObject:@"1011" forKey:@"B"];

    [hexDic setObject:@"1100" forKey:@"C"];

    [hexDic setObject:@"1101" forKey:@"D"];

    [hexDic setObject:@"1110" forKey:@"E"];

    [hexDic setObject:@"1111" forKey:@"F"];

    NSMutableString *binaryString=[[NSMutableString alloc] init];

    for (int i=0; i<[self length]; i++) {

        NSRange rage;

        rage.length = 1;

        rage.location = i;

        NSString *key = [self substringWithRange:rage];

        binaryString = (NSMutableString *)[NSString stringWithFormat:@"%@%@",binaryString,[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]];

    }

    return binaryString;
}

//NSData转换为16进制字符串,NSData的分类
- (NSString *)dataToHexString
{
    NSUInteger          len = [self length];
    char *              chars = (char *)[self bytes];
    NSMutableString *   hexString = [[NSMutableString alloc] init];

    for(NSUInteger i = 0; i < len; i++ )
        [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];

    return hexString;
}

2016.8.29补充

由于项目中需要做关于后台持续扫描,类似于常见的蓝牙音箱,打开手机APP连接蓝牙音箱,播放音乐,当手机远离蓝牙音箱后,停止播放,当手机靠近的时候,蓝牙音箱又开始播放了,对于这中需求的实现我开始很困惑,蓝牙如何后台持续扫描呢,我尝试了很多方法是失败的,经过我多方面查询资料弄清楚如何实现这个需求:

1.需要后台运行需申请后台权限



勾选即可拥有后台权限,如果外设持续发送数据,APP端可以接收到数据。

2.扫描时需指定serviceUUID,需外设广播出自己的SeviceUUID,APP端作为扫描的条件。

这是苹果扫描方法的的官方解释:

Applications that have specified the bluetooth-central background mode are allowed to scan while backgrounded, with two

  • caveats: the scan must specify one or more service types in serviceUUIDs, and the CBCentralManagerScanOptionAllowDuplicatesKey
  • scan option will be ignored.

显而易见的说的很清楚,后台模式时蓝牙作为central,必须指定serviceUUIDs,scan option忽略。


例子

扫描方法:

[self.centralManger scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FEE0"],[CBUUID UUIDWithString:@"FEE7"]] options:nil];

这样当在后台的时候是可以持续扫描的。

3.当后台断开连接时候会调用系统方法

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error

我们需要在这里设置自动重连,即可实现上述的需求。

2016.10.19补充

关于蓝牙需要做一些补充及修改,首先对于后台扫描时候,我们申请后台权限只需要申请


权限申请

如果两个都申请,如果提交安装包到APPStore,你会被拒绝

If your app is meant to work with external hardware, supported protocols must be included in the UISupportedExternalAccessoryProtocols key in your app's Info.plist file - and the hardware's PPID # should be provided in the Review Notes field of your app in iTunes Connect.
Additionally, your app must be authorized by MFi to use the desired hardware. If you are not yet in the MFi Program, you can enroll at MFi program.
Please either revise your Info.plist to include the UISupportedExternalAccessoryProtocols key and update your Review Notes to include the PPID # - or remove the external-accessory value from the UIBackgroundModes key.

posted @ 2017-03-28 14:15  稻草人11223  阅读(1980)  评论(0编辑  收藏  举报
返回顶部