在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:GJLightBlueTooth开源软件地址:https://gitee.com/cowa/GJLightBlueTooth开源软件介绍:GJLightBlueTooth
effect效果图可以在我的github上看到:https://github.com/manofit/GJLightBlueToothdemoYou can try to use project architectureThe architecture of project is: user ——> GJLightBlueTooth ——> CoreBlueTooth ——> GJLightBlueTooth ——> user. There:
In Demo, you can see the class MyBLETool, this aim to seperate page and service, you can call it Device-Class. So we needn't to set callback block in ViewControllers. how to useYou should init GJLightBlueTooth after create ViewController, After get Characteristic, you'd better mate CBCharacteristic in device with Characteristic you get, then save it in local for writing and reading. [self.BLE setBlockWhenDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) { strongify(self); for (CBCharacteristic *cha in service.characteristics){ if ([cha.UUID.UUIDString isEqualToString:CharacteristicUUIDWrite]){ self.writeCharacter = cha; } } //[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoverCharacteristics" object:service]; }]; scan[self.BLE scan] stop scan[self.BLE stopScan] connect[self.BLE connectWithPeripheral:peri] cancel connect[self.BLE cancelConnectWithPeripheral:peri] read RSSI[self.BLE readRSSIWithPeriperal:peri] send command[self.BLE sendDataToPeriperal:peri WriteCharacteristic:self.writeCharacter Command:command NSEncoding:encoding] reconnect & cancel reconnect[self.BLE addReconnectPeriphearal:peri];[self.BLE deleteReconnectPeriphearal:peri]; If you like to keep a heartbeat with device, there is a new thread. you can set max concurrent operation count by yourself. NSData *cmdData = [[NSString stringWithFormat:@"%@",command] dataUsingEncoding:encoding]; NSOperation *opration = [NSBlockOperation blockOperationWithBlock:^{ [peripheral writeValue:cmdData forCharacteristic:writeCharacteristics type:CBCharacteristicWriteWithoutResponse]; /* * you can set thread time interval.but the order while delay when there are a lot of orders. */ //[NSThread sleepForTimeInterval:SleepTimeGap]; }]; [self.writeQueue addOperation:opration]; You can also set time interval between orders, but the orders will delay if you update heartbeat quickly. warning
char nameKey;- (void)setRssi:(NSNumber *)rssi{ objc_setAssociatedObject(self, &nameKey, rssi, OBJC_ASSOCIATION_COPY_NONATOMIC);}- (NSNumber *)rssi{ return objc_getAssociatedObject(self, &nameKey);}
weakify(self);[self.BLE setBlockWhenDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) { strongify(self); for (CBCharacteristic *cha in service.characteristics){ if ([cha.UUID.UUIDString isEqualToString:CharacteristicUUIDWrite]){ self.writeCharacter = cha; } } //[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoverCharacteristics" object:service]; }]; GJLightBlueTooth
demo你可以在Demo中查看如何使用 project architecture整个蓝牙库的架构是:用户 ——> GJLightBlueTooth ——> CoreBlueTooth ——> GJLightBlueTooth ——> 用户。 其中:
而在Demo中,你还会看到MyBLETool这个类,这是为了将Demo项目中页面与业务分离而单独出来的一个类,可以理解为设备类。为了我们不需要在具体的页面中去设置回传的block。 how to use在创建页面后,你应该初始化 在获取到Characteristic后,你应该根据实际读写的Characteristic匹配出设备上的CBCharacteristic,保存在本地,用于后面的写与读。 [self.BLE setBlockWhenDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) { strongify(self); for (CBCharacteristic *cha in service.characteristics){ if ([cha.UUID.UUIDString isEqualToString:CharacteristicUUIDWrite]){ self.writeCharacter = cha; } } //[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoverCharacteristics" object:service]; }]; scan[self.BLE scan] stop scan[self.BLE stopScan] connect[self.BLE connectWithPeripheral:peri] cancel connect[self.BLE cancelConnectWithPeripheral:peri] read RSSI[self.BLE readRSSIWithPeriperal:peri] send command[self.BLE sendDataToPeriperal:peri WriteCharacteristic:self.writeCharacter Command:command NSEncoding:encoding] 添加断开重连 & 取消断开重连[self.BLE addReconnectPeriphearal:peri];[self.BLE deleteReconnectPeriphearal:peri]; 这里针对现在很多公司提出需要手机与设备有心跳的要求,开启了一个线程队列。该队列设置能够同时存在的指令数为3。 NSData *cmdData = [[NSString stringWithFormat:@"%@",command] dataUsingEncoding:encoding]; NSOperation *opration = [NSBlockOperation blockOperationWithBlock:^{ [peripheral writeValue:cmdData forCharacteristic:writeCharacteristics type:CBCharacteristicWriteWithoutResponse]; /* * you can set thread time interval.but the order while delay when there are a lot of orders. */ //[NSThread sleepForTimeInterval:SleepTimeGap]; }]; [self.writeQueue addOperation:opration]; 你也可以设置指令间隔时间,但是这样会造成因心跳刷新过快造成的延迟发送。 warning
char nameKey;- (void)setRssi:(NSNumber *)rssi{ objc_setAssociatedObject(self, &nameKey, rssi, OBJC_ASSOCIATION_COPY_NONATOMIC);}- (NSNumber *)rssi{ return objc_getAssociatedObject(self, &nameKey);}
weakify(self);[self.BLE setBlockWhenDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) { strongify(self); for (CBCharacteristic *cha in service.characteristics){ if ([cha.UUID.UUIDString isEqualToString:CharacteristicUUIDWrite]){ self.writeCharacter = cha; } } //[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoverCharacteristics" object:service]; }]; |
请发表评论