iOS-BLE蓝牙开发

在写这个博客之前,空余时间抽看了近一个月的文档和Demo,系统给的解释很详细,接口也比较实用,唯独有一点,对于设备的唯一标示,网上众说

纷纭,在这里我目前也还没有自己的见解,只是在不断的测试各种情况,亲测同一设备的UUID对于每台iPhone设备都不一样,只能尽量保证设备的唯一性,特别是自动重连的过程,让用户没有感知。我之前也找了很久,发现CBCentralManager和CBPeripheral里边都找不到和Mac地址有关的东西,后来发现一般是外设在Device Information服务中的某个特征返回的。经过与硬件工程师的协商,决定APP端将从这个服务中获取到蓝牙设备以及我的iPhone手机的蓝牙Mac地址,为自动连接的唯一性做准备。

这里经过和硬件工程师的测试,发现设备端在获取手机蓝牙MAC地址的时候,当用户手机重启之后,这个地址也是会随机变化的,也就是说,作为开发者,只有设备的MAC地址能够保持唯一性不变化。

ble.png

有疑问的朋友可以先去这里瞅一瞅

一个关于蓝牙4.0的智能硬件Demo详解

下面是两台iPhone6连接同一台蓝牙设备的结果:

**成功连接**** peripheral:with UUID:<__NSConcreteUUID0x17003d980>50084F69-BA5A-34AC-8A6E-6F0CEADB21CD************成功连接**** peripheral:with UUID:<__NSConcreteUUID0x174036c00>55B7D759-0F1E-6271-EA14-BC5A9C9EEEEC**

进入正题

iOS的蓝牙开发很简单,只要包含一个库,创建CBCentralManager实例,实现代理方法,然后就可以直接和设备进行通信。

发现附近的特定蓝牙设备

#import

首先可以定义一些即将使用到的UUID的宏

#definekPeripheralName    @"360qws Electric Bike Service"//外围设备名称#definekServiceUUID        @"7CACEB8B-DFC4-4A40-A942-AAD653D174DC"//服务的UUID#definekCharacteristicUUID @"282A67B2-8DAB-4577-A42F-C4871A3EEC4F"//特征的UUID

如果不是把手机作为中心设备的话,这些没有必要设置。

这里我也没有用到,仅仅是提了一下,具体操作后续添加。

对于生成UUID,大家可以谷歌一下,直接通过mac终端生成32位UUID。

1.声明属性

@property(weak,nonatomic)IBOutletUITableView*bluetoothTable;@property(weak,nonatomic)IBOutletUITextView*resultTextView;@propertyBOOLcbReady;@property(nonatomic)floatbatteryValue;@property(nonatomic,strong) CBCentralManager *manager;@property(nonatomic,strong) CBPeripheral *peripheral;@property(strong,nonatomic) CBCharacteristic *writeCharacteristic;@property(strong,nonatomic)NSMutableArray*nDevices;@property(strong,nonatomic)NSMutableArray*nServices;@property(strong,nonatomic)NSMutableArray*nCharacteristics;

2.遵守协议(这里我用到了table)

@interfaceViewController ()

3.初始化数据

- (void)viewDidLoad {    [superviewDidLoad];// Do any additional setup after loading the view, typically from a nib.self.manager = [[CBCentralManager alloc] initWithDelegate:selfqueue:nil];    _cbReady =false;    _nDevices = [[NSMutableArrayalloc]init];    _nServices = [[NSMutableArrayalloc]init];    _nCharacteristics = [[NSMutableArrayalloc]init];    _bluetoothTable.delegate =self;    _bluetoothTable.dataSource =self;    count =0;}

4.实现蓝牙的协议方法

(1)检测蓝牙状态

//开始查看服务,蓝牙开启-(void)centralManagerDidUpdateState:(CBCentralManager *)central{switch(central.state) {caseCBCentralManagerStatePoweredOn:{            [selfupdateLog:@"蓝牙已打开,请扫描外设"];            [_activity startAnimating];            [_managerscanForPeripheralsWithServices:@[[CBUUIDUUIDWithString:@"FF15"]]options:@{CBCentralManagerScanOptionAllowDuplicatesKey :@YES}];        }break;caseCBCentralManagerStatePoweredOff:[selfupdateLog:@"蓝牙没有打开,请先打开蓝牙"];break;default:break;    }}

注:[_manager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FF15"]]  options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];中间的@[[CBUUID UUIDWithString:@"FF15"]]是为了过滤掉其他设备,可以搜索特定标示的设备。

(2)检测到外设后,停止扫描,连接设备

//查到外设后,停止扫描,连接设备-(void)centralManager:(CBCentralManager *)centraldidDiscoverPeripheral:(CBPeripheral *)peripheraladvertisementData:(NSDictionary *)advertisementDataRSSI:(NSNumber *)RSSI{    [selfupdateLog:[NSStringstringWithFormat:@"已发现 peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.identifier, advertisementData]];    _peripheral = peripheral;    [_managerconnectPeripheral:_peripheraloptions:nil];    [self.manager stopScan];    [_activity stopAnimating];    BOOL replace = NO;// Match if we have this device from beforefor(inti=0; i < _nDevices.count; i++) {        CBPeripheral *p = [_nDevicesobjectAtIndex:i];if([pisEqual:peripheral]) {            [_nDevicesreplaceObjectAtIndex:iwithObject:peripheral];            replace = YES;        }    }if(!replace) {        [_nDevicesaddObject:peripheral];        [_bluetoothTable reloadData];    }}

(3)连接外设后的处理

//连接外设成功,开始发现服务- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {NSLog(@"%@", [NSStringstringWithFormat:@"成功连接 peripheral: %@ with UUID: %@",peripheral,peripheral.identifier]);    [selfupdateLog:[NSStringstringWithFormat:@"成功连接 peripheral: %@ with UUID: %@",peripheral,peripheral.identifier]];    [self.peripheral setDelegate:self];    [self.peripheral discoverServices:nil];    [selfupdateLog:@"扫描服务"];}//连接外设失败-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError*)error{NSLog(@"%@",error);}-(void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError*)error{NSLog(@"%s,%@",__PRETTY_FUNCTION__,peripheral);intrssi = abs([peripheral.RSSI intValue]);CGFloatci = (rssi -49) / (10*4.);NSString*length = [NSStringstringWithFormat:@"发现BLT4.0热点:%@,距离:%.1fm",_peripheral,pow(10,ci)];    [selfupdateLog:[NSStringstringWithFormat:@"距离:%@", length]];}

(4)发现服务和搜索到的Characteristice

//已发现服务-(void)peripheral:(CBPeripheral *)peripheraldidDiscoverServices:(NSError *)error{    [selfupdateLog:@"发现服务."];inti=0;for(CBService *sinperipheral.services) {        [self.nServicesaddObject:s];    }for(CBService *sinperipheral.services) {        [selfupdateLog:[NSStringstringWithFormat:@"%d :服务 UUID: %@(%@)",i,s.UUID.data,s.UUID]];        i++;        [peripheraldiscoverCharacteristics:nilforService:s];if([s.UUIDisEqual:[CBUUIDUUIDWithString:@"FF15"]]) {            BOOL replace = NO;// Match if we have this device from beforefor(inti=0; i < _nDevices.count; i++) {                CBPeripheral *p = [_nDevicesobjectAtIndex:i];if([pisEqual:peripheral]) {                    [_nDevicesreplaceObjectAtIndex:iwithObject:peripheral];                    replace = YES;                }            }if(!replace) {                [_nDevicesaddObject:peripheral];                [_bluetoothTable reloadData];            }        }    }}//已搜索到Characteristics-(void)peripheral:(CBPeripheral *)peripheraldidDiscoverCharacteristicsForService:(CBService *)serviceerror:(NSError *)error{    [selfupdateLog:[NSStringstringWithFormat:@"发现特征的服务:%@ (%@)",service.UUID.data ,service.UUID]];for(CBCharacteristic *cinservice.characteristics) {        [selfupdateLog:[NSStringstringWithFormat:@"特征 UUID: %@ (%@)",c.UUID.data,c.UUID]];if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FF01"]]) {            _writeCharacteristic = c;        }if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FF02"]]) {            [_peripheralreadValueForCharacteristic:c];            [_peripheralsetNotifyValue:YESforCharacteristic:c];        }if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FF04"]]) {            [_peripheralreadValueForCharacteristic:c];        }if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FF05"]]) {            [_peripheralreadValueForCharacteristic:c];            [_peripheralsetNotifyValue:YESforCharacteristic:c];        }if([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FFA1"]]) {            [_peripheral readRSSI];        }        [_nCharacteristicsaddObject:c];    }}- (void)centralManager:(CBCentralManager *)centraldidDisconnectPeripheral:(CBPeripheral *)peripheralerror:(NSError *)error {    [selfupdateLog:[NSStringstringWithFormat:@"已断开与设备:[%@]的连接", peripheral.name]];}

(5)获取外设发来的数据

//获取外设发来的数据,不论是read和notify,获取数据都是从这个方法中读取。- (void)peripheral:(CBPeripheral *)peripheraldidUpdateValueForCharacteristic:(CBCharacteristic *)characteristicerror:(NSError *)error{if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FF02"]]) {        NSData * data = characteristic.value;        Byte * resultByte = (Byte *)[data bytes];for(inti=0;i<[data length];i++)            printf("testByteFF02[%d] = %d\n",i,resultByte[i]);if(resultByte[1] ==0) {switch(resultByte[0]) {case3:// 加解锁{if(resultByte[2] ==0) {                        [selfupdateLog:@"撤防成功!!!"];                    }elseif(resultByte[2] ==1) {                        [selfupdateLog:@"设防成功!!!"];                    }                }break;case4:// 开坐桶{if(resultByte[2] ==0) {                        [selfupdateLog:@"关坐桶成功!!!"];                    }elseif(resultByte[2] ==1) {                        [selfupdateLog:@"开坐桶成功!!!"];                    }                }break;case5:// 锁定电机{if(resultByte[2] ==0) {                        [selfupdateLog:@"解锁电机控制器成功!!!"];                    }elseif(resultByte[2] ==1) {                        [selfupdateLog:@"锁定电机控制器成功!!!"];                    }                }break;default:break;            }        }elseif(resultByte[1] ==1) {            [selfupdateLog:@"未知错误"];        }elseif(resultByte[1] ==2) {            [selfupdateLog:@"鉴权失败"];        }    }if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FF04"]]) {        NSData * data = characteristic.value;        Byte * resultByte = (Byte *)[data bytes];for(inti=0;i<[data length];i++)            printf("testByteFF04[%d] = %d\n",i,resultByte[i]);if(resultByte[0] ==0) {// 未绑定 -》写鉴权码[selfupdateLog:@"当前车辆未绑定,请鉴权"];            [self authentication];// 鉴权[selfwritePassword:nilnewPw:nil];        }elseif(resultByte[0] ==1) {// 已绑定 -》鉴权[selfupdateLog:@"当前车辆已经绑定,请鉴权"];            [selfwritePassword:nilnewPw:nil];        }elseif(resultByte[0] ==2) {// 允许绑定[selfupdateLog:@"当前车辆允许绑定"];        }    }if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FF05"]]) {        NSData * data = characteristic.value;        Byte * resultByte = (Byte *)[data bytes];for(inti=0;i<[data length];i++)            printf("testByteFF05[%d] = %d\n",i,resultByte[i]);if(resultByte[0] ==0) {// 设备加解锁状态 0 撤防    1 设防[selfupdateLog:@"当前车辆撤防状态"];        }elseif(resultByte[0] ==1) {// 设备加解锁状态 0 撤防    1 设防[selfupdateLog:@"当前车辆设防状态"];        }    }}//中心读取外设实时数据- (void)peripheral:(CBPeripheral *)peripheraldidUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristicerror:(NSError *)error {if(error) {        NSLog(@"Error changing notification state: %@", error.localizedDescription);    }// Notification has startedif(characteristic.isNotifying) {        [peripheralreadValueForCharacteristic:characteristic];    }else{// Notification has stopped// so disconnect from the peripheralNSLog(@"Notification stopped on %@.  Disconnecting", characteristic);        [selfupdateLog:[NSStringstringWithFormat:@"Notification stopped on %@.  Disconnecting", characteristic]];        [self.managercancelPeripheralConnection:self.peripheral];    }}//用于检测中心向外设写数据是否成功-(void)peripheral:(CBPeripheral *)peripheraldidWriteValueForCharacteristic:(CBCharacteristic *)characteristicerror:(NSError *)error{if(error) {        NSLog(@"=======%@",error.userInfo);        [selfupdateLog:[error.userInfo JSONString]];    }else{        NSLog(@"发送数据成功");        [selfupdateLog:@"发送数据成功"];    }/* When a write occurs, need to set off a re-read of the local CBCharacteristic to update its value */[peripheralreadValueForCharacteristic:characteristic];}

(6)其他辅助性的

#pragma mark - 蓝牙的相关操作- (IBAction)bluetoothAction:(UIButton*)sender {switch(sender.tag) {case201:        {// 搜索设备[selfupdateLog:@"正在扫描外设..."];            [_activity startAnimating];            [_manager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FF15"]]  options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];doubledelayInSeconds =30.0;            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds *NSEC_PER_SEC));            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){                [self.manager stopScan];                [_activity stopAnimating];                [selfupdateLog:@"扫描超时,停止扫描"];            });        }break;case202:        {// 连接if(_peripheral && _cbReady) {                [_manager connectPeripheral:_peripheral options:nil];                _cbReady =NO;            }        }break;case203:        {// 断开if(_peripheral && !_cbReady) {                [_manager cancelPeripheralConnection:_peripheral];                _cbReady =YES;            }        }break;case204:        {// 暂停搜索[self.manager stopScan];        }break;case211:        {// 车辆上锁[selflock];        }break;case212:        {// 车辆解锁[selfunLock];        }break;case213:        {// 开启坐桶[selfopen];        }break;case214:        {// 立即寻车[selffind];        }break;default:break;    }}-(NSOperationQueue*)queue {if(!_queue) {// 请求队列self.queue = [[NSOperationQueuealloc]init];        [self.queue setMaxConcurrentOperationCount:1];    }return_queue;}#pragma mark - 命令#pragma mark - 鉴权-(void)authentication {    Byte byte[] = {1,1,2,3,4,5,6,7,8};if(_peripheral.state == CBPeripheralStateConnected) {        [_peripheral writeValue:[NSDatadataWithBytes:byte length:9] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];    }}#pragma mark - 写密码-(void)writePassword:(NSString*)initialPw newPw:(NSString*)newPw {    Byte byte[] = {2,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8};if(_peripheral.state == CBPeripheralStateConnected) {        [_peripheral writeValue:[NSDatadataWithBytes:byte length:17] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];    }}#pragma mark - 车辆上锁-(void)lock {    Byte byte[] = {3,1};if(_peripheral.state == CBPeripheralStateConnected) {        [_peripheral writeValue:[NSDatadataWithBytes:byte length:2] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];    }}#pragma mark - 车辆解锁-(void)unLock {    Byte byte[] = {3,0};if(_peripheral.state == CBPeripheralStateConnected) {        [_peripheral writeValue:[NSDatadataWithBytes:byte length:2] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];    }}#pragma mark - 开启坐桶-(void)open {    Byte byte[] = {4,1};if(_peripheral.state == CBPeripheralStateConnected) {        [_peripheral writeValue:[NSDatadataWithBytes:byte length:2] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];    }}#pragma mark - 立即寻车-(void)find {    Byte byte[] = {7};if(_peripheral.state == CBPeripheralStateConnected) {        [_peripheral writeValue:[NSDatadataWithBytes:byte length:1] forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithoutResponse];    }}-(NSData*)hexString:(NSString*)hexString {intj=0;    Byte bytes[20];///3ds key的Byte 数组, 128位for(inti=0; i<[hexString length]; i++)    {intint_ch;/// 两位16进制数转化后的10进制数unicharhex_char1 = [hexString characterAtIndex:i];////两位16进制数中的第一位(高位*16)intint_ch1;if(hex_char1 >='0'&& hex_char1 <='9')            int_ch1 = (hex_char1-48)*16;//// 0 的Ascll - 48elseif(hex_char1 >='A'&& hex_char1 <='F')            int_ch1 = (hex_char1-55)*16;//// A 的Ascll - 65elseint_ch1 = (hex_char1-87)*16;//// a 的Ascll - 97i++;unicharhex_char2 = [hexString characterAtIndex:i];///两位16进制数中的第二位(低位)intint_ch2;if(hex_char2 >='0'&& hex_char2 <='9')            int_ch2 = (hex_char2-48);//// 0 的Ascll - 48elseif(hex_char1 >='A'&& hex_char1 <='F')            int_ch2 = hex_char2-55;//// A 的Ascll - 65elseint_ch2 = hex_char2-87;//// a 的Ascll - 97int_ch = int_ch1+int_ch2;NSLog(@"int_ch=%d",int_ch);        bytes[j] = int_ch;///将转化后的数放入Byte数组里j++;    }NSData*newData = [[NSDataalloc] initWithBytes:bytes length:20];returnnewData;}

在和硬件之间的数据发送和接受,用的都是byte数组。最后,添加一个存储已连接过得设备

- (void) addSavedDevice:(CFUUIDRef)uuid {NSArray*storedDevices = [[NSUserDefaultsstandardUserDefaults] arrayForKey:@"StoredDevices"];NSMutableArray*newDevices =nil;CFStringRefuuidString =NULL;if(![storedDevices isKindOfClass:[NSArrayclass]]) {NSLog(@"Can't find/create an array to store the uuid");return;    }    newDevices = [NSMutableArrayarrayWithArray:storedDevices];    uuidString =CFUUIDCreateString(NULL, uuid);if(uuidString) {        [newDevices addObject:(__bridgeNSString*)uuidString];CFRelease(uuidString);    }/* Store */[[NSUserDefaultsstandardUserDefaults] setObject:newDevices forKey:@"StoredDevices"];    [[NSUserDefaultsstandardUserDefaults] synchronize];}- (void) loadSavedDevices {NSArray*storedDevices = [[NSUserDefaultsstandardUserDefaults] arrayForKey:@"StoredDevices"];if(![storedDevices isKindOfClass:[NSArrayclass]]) {NSLog(@"No stored array to load");return;    }for(iddeviceUUIDStringinstoredDevices) {if(![deviceUUIDString isKindOfClass:[NSStringclass]])continue;CFUUIDRefuuid =CFUUIDCreateFromString(NULL, (CFStringRef)deviceUUIDString);if(!uuid)continue;        [CBCentralManager retrievePeripherals:[NSArrayarrayWithObject:(__bridgeid)uuid]];CFRelease(uuid);    }}//And the delegate function for the Retrieve Peripheral- (void) centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray*)peripherals {    CBPeripheral *peripheral;/* Add to list. */for(peripheralinperipherals) {        [central connectPeripheral:peripheral options:nil];    }} - (void) centralManager:(CBCentralManager *)central didRetrievePeripheral:(CBPeripheral *)peripheral {    [central connectPeripheral:peripheral options:nil];}

后记

最主要是用UUID来确定你要干的事情,特征和服务的UUID都是外设定义好的。我们只需要读取,确定你要读取什么的时候,就去判断UUID是否相符。 一般来说我们使用的iPhone都是做centralManager的,蓝牙模块是peripheral的,所以我们是want datas,需要接受数据。

1.判断状态为powerOn,然后执行扫描

2.停止扫描,连接外设

3.连接成功,寻找服务

4.在服务里寻找特征

5.为特征添加通知

5.通知添加成功,那么就可以实时的读取value[也就是说只要外设发送数据[一般外设的频率为10Hz],代理就会调用此方法]。

6.处理接收到的value,[hex值,得转换] 之后就自由发挥了,在这期间都是通过代理来实现的,也就是说你只需要处理你想要做的事情,代理会帮你调用方法。[别忘了添加代理]

2015-07-28 更

关于write我这里还有些注意的地方要强调!!!!

并不是每一个Characteristic都可以通过回调函数来查看它写入状态的。就比如针对 immediateAlertService(1802) 的 alertLevelCharacteristic(2A06),就是一个不能有response的Characteristic。刚开始我就一直用CBCharacteristicWriteType.WithResponse来进行写入始终不成功,郁闷坏了,最后看到每个Characteristic还有个属性值是指示这个的,我将每个Characteristic打印出来有如下信息:

immediateAlertService Discover characteristiclinkLossAlertService Discover characteristic

这个的properties是什么刚开始不知道,觉得他没意义,后面才注意到properties是Characteristic的一个参数,具体解释如下:

DeclarationSWIFTstruct CBCharacteristicProperties :RawOptionSetType{    init(_value: UInt)    var value: UInt    static var Broadcast: CBCharacteristicProperties { get }staticvarRead:CBCharacteristicProperties{ get }staticvarWriteWithoutResponse:CBCharacteristicProperties{ get }staticvarWrite:CBCharacteristicProperties{ get }staticvarNotify:CBCharacteristicProperties{ get }staticvarIndicate:CBCharacteristicProperties{ get }staticvarAuthenticatedSignedWrites:CBCharacteristicProperties{ get }staticvarExtendedProperties:CBCharacteristicProperties{ get }staticvarNotifyEncryptionRequired:CBCharacteristicProperties{ get }staticvarIndicateEncryptionRequired:CBCharacteristicProperties{ get }}OBJECTIVE-Ctypedefenum{  CBCharacteristicPropertyBroadcast =0x01,  CBCharacteristicPropertyRead =0x02,  CBCharacteristicPropertyWriteWithoutResponse =0x04,  CBCharacteristicPropertyWrite =0x08,  CBCharacteristicPropertyNotify =0x10,  CBCharacteristicPropertyIndicate =0x20,  CBCharacteristicPropertyAuthenticatedSignedWrites =0x40,  CBCharacteristicPropertyExtendedProperties =0x80,  CBCharacteristicPropertyNotifyEncryptionRequired =0x100,  CBCharacteristicPropertyIndicateEncryptionRequired =0x200,}CBCharacteristicProperties;

可以看到0x04对应的是CBCharacteristicPropertyWriteWithoutResponse

0x0A对应的是CBCharacteristicPropertyNotify

所以 immediateAlertService(1802) 的 alertLevelCharacteristic(2A06)是不能用CBCharacteristicWriteType.WithRespons进行写入,只能用CBCharacteristicWriteType.WithOutRespons。这样在以后的开发中可以对每个Characteristic的这个参数进行检查再进行设置。

最后讲一下关于蓝牙绑定的过程,在iOS中,没有讲当绑定的过程,直接就是扫描、连接、交互。从而很多人会认为,连接就是绑定了,其实不然。在iOS开发中,连接并没有完成绑定,在网上找到了个很好的解释:

you cannot initiate pairing from the iOS central side. Instead, you have to read/write a characteristic value,

and then let your peripheral respond with an "Insufficient Authentication" error.

iOS will then initiate pairing, will store the keys for later use (bonding) and encrypts the link. As far as I know,

it also caches discovery information, so that future connections can be set up faster.

就是当发生读写交互时,系统在会和外设进行绑定操作!!!

2016-02-20 更

ios蓝牙如何获取广播包数据

如题,手机作为主设备,在使用CoreBluetooth时候,想获取蓝牙的数据广播包。在使用

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)aPeripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

方法时候获取的advertisementData打印出来只有

******kCBAdvDataIsConnectable****kCBAdvDataLocalName****kCBAdvDataManufacturerData**

三个属性对应的值,但这并非广播包的数据。例如安卓可以通过scandata来获取到广播包的值,那么iOS这边我应该怎么做呢?

好像苹果这边禁止读取这种广播内容的的,真要的话你可以让硬件那边把数据做到kCBAdvDataManufacturerData这个字段里面。

Demo地址:一个蓝牙4.0的智能硬件Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容