夏一

人生是不断成就完善的过程。

蓝牙模块

现在可穿戴设备越来越多,特别是applewatch出来之后,各种的watch都推出了。这样连接就显得非常重要了,目前主流的连接还是蓝牙。因此蓝牙这方面的技能也是大家需要拓展的小知识。今天趁有点时间把这方面的知识整理一下。方便大家相互学习查阅。

说到蓝牙,首先要说一下蓝牙2.0,在苦逼的2.0时代,只能使用MFI认证的蓝牙模块设计你的蓝牙产品。因为其它未经认证的是不能被检测到的。除非用户越狱。那么什么是MFI认证呢?意思是(Make Foripod/ipad/iphone),只有少数的硬件厂商才有苹果的MFI认证。但试问自己做的产品要有多牛才能让用户为此越狱呢。所以一个MFI认证很多的iOS软件开发公司望而却步。

还有一点需要注意的是iOS是的蓝牙从出生开始就是对文件传输是绝对的拒绝的。原因很简单,因为非越狱苹果上面听歌不都是要花钱的么,如果你用蓝牙把歌曲传给别人,那不是侵权了么(当然,在TC没有侵权一说),所以,苹果在硬件上面就把你传输文件给限制了。苹果之所以给他们装上蓝牙不过是让你练练蓝牙耳机啊,蓝牙音箱之类的。

但是,在蓝牙4.0出来之后(注意,硬件上要4s以上,系统要ios6以上才能支持4.0),苹果开放了BLE通道,真是屌丝的福音啊,我们这些没有MFI认证的蓝牙设备终于可以连接非越狱的苹果了,所以也可以开发蓝牙应用啦。但是,这得是蓝牙4.0才有的,也就是说爱疯4也不支持哦,所以完全兼容还是没可能。

蓝牙4.0的出现突然之间让我们拥有了很多。我们可以畅快地开发自己蓝牙产品啦。

接下来我们进入正题。蓝牙开发一般分为以下五步。

1建立中心角色

#import

CBCentralManager *manager;

manager = [[CBCentralManager alloc]initWithDelegate:selfqueue:nil];

2扫描外设(discover)

[manager scanForPeripheralsWithServices:niloptions:options];

3连接外设(connect)

- (void)centralManager:(CBCentralManager *)centraldidDiscoverPeripheral:(CBPeripheral *)peripheraladvertisementData:(NSDictionary*)advertisementDataRSSI:(NSNumber*)RSSI

{

if([peripheral.name isEqualToString:BLE_SERVICE_NAME]){

[selfconnect:peripheral];

}

s);

}

-(BOOL)connect:(CBPeripheral *)peripheral{

self.manager.delegate =self;

[self.managerconnectPeripheral:peripheraloptions:[NSDictionarydictionaryWithObject:[NSNumbernumberWithBool:YES]forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];

}

4扫描外设中的服务和特征(discover)

- (void)centralManager:(CBCentralManager *)centraldidConnectPeripheral:(CBPeripheral *)peripheral

{

NSLog(@"Did connect to peripheral: %@", peripheral);

_testPeripheral = peripheral;

[peripheral setDelegate:self]; 
//查找服务

[peripheral discoverServices:nil];

}


 

- (void)peripheral:(CBPeripheral *)peripheraldidDiscoverServices:(NSError*)error

{

NSLog(@"didDiscoverServices");

if(error)

{

NSLog(@"Discovered services for %@ with error: %@",peripheral.name, [error localizedDescription]);

if([self.delegaterespondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)])

[self.delegateDidNotifyFailConnectService:nilwithPeripheral:nilerror:nil];

return;

}

for(CBService *service in peripheral.services)

{

//发现服务

if([service.UUID isEqual:[CBUUIDUUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]])

{

NSLog(@"Service found with UUID: %@",service.UUID); 
//查找特征

[peripheral discoverCharacteristics:nilforService:service];

break;

       }

    }

}

- (void)peripheral:(CBPeripheral *)peripheraldidDiscoverCharacteristicsForService:(CBService *)serviceerror:(NSError*)error

{

if(error)

{

NSLog(@"Discovered characteristics for %@ with error: %@",service.UUID, [error localizedDescription]);

[selferror];

return;

}

NSLog(@"服务:%@",service.UUID);

for(CBCharacteristic *characteristic inservice.characteristics)

{

//发现特征

if([characteristic.UUID isEqual:[CBUUIDUUIDWithString:@"xxxxxxx"]]) {

NSLog(@"监听:%@",characteristic);
//监听特征

[self.peripheralsetNotifyValue:YESforCharacteristic:characteristic];

         }

    }

}

5与外设做数据交互(读 与 写)


- (void)peripheral:(CBPeripheral *)peripheraldidUpdateValueForCharacteristic:(CBCharacteristic *)characteristicerror:(NSError*)error

{

if(error)

{

NSLog(@"Error updating value for characteristic %@ error: %@",characteristic.UUID, [error localizedDescription]);

self.error_b = BluetoothError_System;

[selferror];

return;

}

//   NSLog(@"收到的数据:%@",characteristic.value);

[selfdecodeData:characteristic.value];

}


NSData*d2 = [[PBABluetoothDecode sharedManager]HexStringToNSData:@"0x02"];

[self.peripheral writeValue:d2 forCharacteristic:characteristictype:CBCharacteristicWriteWithoutResponse];

今天就先到这里,大家有什么新的增加知识点,再一起研究。博客对于我们搞开发的人来说是个好工具。可以自己整理复习一些知识,也可以让阅读者学到一些知识。所以在微博无处不在的时代,我们仍然畅快地书写长篇的博客。同志们加油!


评论