菜鳥 本人再做一個(gè)項(xiàng)目,一個(gè) 類似血糖儀的 設(shè)備糜俗,需要通過藍(lán)牙實(shí)時(shí)傳輸測(cè)量的值然后根據(jù)數(shù)值變化 去改變界面的值和波形圖踱稍,本來查找了 很多demo曲饱,第三方框架,都是只能發(fā)現(xiàn)是設(shè)備但是拿不到實(shí)時(shí)變化的值把本菜鳥一頓愁啊珠月,然后各種找資料 扩淀,外文網(wǎng)站 啊 ,最后 通過使用<babybluetooth>的框架桥温,并且修改了 部分代碼引矩,拿到實(shí)時(shí)變化的值,
主要是 修改圖中讀取數(shù)值的方式即可侵浸,這個(gè)特別重要M隆!L途酢G恕!
接下來是修改的 核心類里面的代碼 :
#import"BabyCentralManager.h"
#import"BabyCallback.h"
@implementationBabyCentralManager
#define currChannel [babySpeaker callbackOnCurrChannel]
- (instancetype)init {
self= [superinit];
if(self) {
#if__IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0
NSDictionary*options = [NSDictionarydictionaryWithObjectsAndKeys:
//藍(lán)牙power沒打開時(shí)alert提示框
[NSNumbernumberWithBool:YES],CBCentralManagerOptionShowPowerAlertKey,
//重設(shè)centralManager恢復(fù)的IdentifierKey
@"babyBluetoothRestore",CBCentralManagerOptionRestoreIdentifierKey,
nil];
#else
NSDictionary *options =nil;
#endif
NSArray*backgroundModes = [[[NSBundlemainBundle]infoDictionary]objectForKey:@"UIBackgroundModes"];
if([backgroundModescontainsObject:@"bluetooth-central"]) {
//后臺(tái)模式
centralManager= [[CBCentralManageralloc]initWithDelegate:selfqueue:niloptions:options];
}
else{
//非后臺(tái)模式
centralManager= [[CBCentralManageralloc]initWithDelegate:selfqueue:nil];
}
pocket= [[NSMutableDictionaryalloc]init];
connectedPeripherals= [[NSMutableArrayalloc]init];
discoverPeripherals= [[NSMutableArrayalloc]init];
reConnectPeripherals= [[NSMutableArrayalloc]init];
}
returnself;
}
#pragma mark -接收到通知
//掃描Peripherals
- (void)scanPeripherals {
[centralManagerscanForPeripheralsWithServices:[currChannelbabyOptions].scanForPeripheralsWithServicesoptions:[currChannelbabyOptions].scanForPeripheralsWithOptions];
}
//連接Peripherals
- (void)connectToPeripheral:(CBPeripheral*)peripheral{
[centralManagerconnectPeripheral:peripheraloptions:[currChannelbabyOptions].connectPeripheralWithOptions];
}
//斷開設(shè)備連接
- (void)cancelPeripheralConnection:(CBPeripheral*)peripheral {
[centralManagercancelPeripheralConnection:peripheral];
}
//斷開所有已連接的設(shè)備
- (void)cancelAllPeripheralsConnection {
for(inti=0;i
[centralManagercancelPeripheralConnection:connectedPeripherals[i]];
}
}
//停止掃描
- (void)cancelScan {
[centralManagerstopScan];
//停止掃描callback
if([currChannelblockOnCancelScan]) {
[currChannelblockOnCancelScan](centralManager);
}
}
#pragma mark - CBCentralManagerDelegate委托方法
- (void)centralManagerDidUpdateState:(CBCentralManager*)central {
//發(fā)送通知
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtCentralManagerDidUpdateStateobject:@{@"central":central}];
switch(central.state) {
caseCBCentralManagerStateUnknown:
BabyLog(@">>>CBCentralManagerStateUnknown");
break;
caseCBCentralManagerStateResetting:
BabyLog(@">>>CBCentralManagerStateResetting");
break;
caseCBCentralManagerStateUnsupported:
BabyLog(@">>>CBCentralManagerStateUnsupported");
break;
caseCBCentralManagerStateUnauthorized:
BabyLog(@">>>CBCentralManagerStateUnauthorized");
break;
caseCBCentralManagerStatePoweredOff:
BabyLog(@">>>CBCentralManagerStatePoweredOff");
break;
caseCBCentralManagerStatePoweredOn:
BabyLog(@">>>CBCentralManagerStatePoweredOn");
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtCentralManagerEnableobject:@{@"central":central}];
break;
default:
break;
}
//狀態(tài)改變callback
if([currChannelblockOnCentralManagerDidUpdateState]) {
[currChannelblockOnCentralManagerDidUpdateState](central);
}
}
- (void)centralManager:(CBCentralManager*)central willRestoreState:(NSDictionary*)dict {
}
//掃描到Peripherals
- (void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary*)advertisementData RSSI:(NSNumber*)RSSI {
//日志
//BabyLog(@"當(dāng)掃描到設(shè)備:%@",peripheral.name);
[selfaddDiscoverPeripheral:peripheral];
//發(fā)出通知
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidDiscoverPeripheral
object:@{@"central":central,@"peripheral":peripheral,@"advertisementData":advertisementData,@"RSSI":RSSI}];
//掃描到設(shè)備callback
if([currChannelfilterOnDiscoverPeripherals]) {
if([currChannelfilterOnDiscoverPeripherals](peripheral.name,advertisementData,RSSI)) {
if([currChannelblockOnDiscoverPeripherals]) {
[[babySpeakercallbackOnCurrChannel]blockOnDiscoverPeripherals](central,peripheral,advertisementData,RSSI);
}
}
}
//處理連接設(shè)備
if(needConnectPeripheral) {
if([currChannelfilterOnconnectToPeripherals](peripheral.name,advertisementData,RSSI)) {
[centralManagerconnectPeripheral:peripheraloptions:[currChannelbabyOptions].connectPeripheralWithOptions];
//開一個(gè)定時(shí)器監(jiān)控連接超時(shí)的情況
connectTimer= [NSTimerscheduledTimerWithTimeInterval:5.0ftarget:selfselector:@selector(disconnect:)userInfo:peripheralrepeats:NO];
}
}
}
//停止掃描
- (void)disconnect:(id)sender {
[centralManagerstopScan];
}
//連接到Peripherals-成功
- (void)centralManager:(CBCentralManager*)central didConnectPeripheral:(CBPeripheral*)peripheral {
//發(fā)出通知
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidConnectPeripheral
object:@{@"central":central,@"peripheral":peripheral}];
//設(shè)置委托
[peripheralsetDelegate:self];
//BabyLog(@">>>連接到名稱為(%@)的設(shè)備-成功",peripheral.name);
[connectTimerinvalidate];//停止時(shí)鐘
[selfaddPeripheral:peripheral];
//執(zhí)行回叫
//掃描到設(shè)備callback
if([currChannelblockOnConnectedPeripheral]) {
[currChannelblockOnConnectedPeripheral](central,peripheral);
}
//掃描外設(shè)的服務(wù)
if(needDiscoverServices) {
[peripheraldiscoverServices:[currChannelbabyOptions].discoverWithServices];
//discoverIncludedServices
}
}
//連接到Peripherals-失敗
- (void)centralManager:(CBCentralManager*)central didFailToConnectPeripheral:(CBPeripheral*)peripheral error:(NSError*)error {
//發(fā)出通知
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidFailToConnectPeripheral
object:@{@"central":central,@"peripheral":peripheral,@"error":error?error:@""}];
//BabyLog(@">>>連接到名稱為(%@)的設(shè)備-失敗,原因:%@",[peripheral name],[error localizedDescription]);
if([currChannelblockOnFailToConnect]) {
[currChannelblockOnFailToConnect](central,peripheral,error);
}
}
//Peripherals斷開連接
- (void)centralManager:(CBCentralManager*)central didDisconnectPeripheral:(CBPeripheral*)peripheral error:(NSError*)error {
//發(fā)出通知
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidDisconnectPeripheral
object:@{@"central":central,@"peripheral":peripheral,@"error":error?error:@""}];
//BabyLog(@">>>外設(shè)連接斷開連接%@: %@\n", [peripheral name], [error localizedDescription]);
if(error)
{
BabyLog(@">>> didDisconnectPeripheral for %@ with error: %@", peripheral.name, [error localizedDescription]);
}
[selfdeletePeripheral:peripheral];
if([currChannelblockOnDisconnect]) {
[currChannelblockOnDisconnect](central,peripheral,error);
}
//判斷是否全部鏈接都已經(jīng)段開,調(diào)用blockOnCancelAllPeripheralsConnection委托
if([selffindConnectedPeripherals].count==0) {
//停止掃描callback
if([currChannelblockOnCancelAllPeripheralsConnection]) {
[currChannelblockOnCancelAllPeripheralsConnection](centralManager);
}
//BabyLog(@">>> stopConnectAllPerihperals");
}
//檢查并重新連接需要重連的設(shè)備
if([reConnectPeripheralscontainsObject:peripheral]) {
[selfconnectToPeripheral:peripheral];
}
}
//掃描到服務(wù)
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverServices:(NSError*)error {
//發(fā)出通知
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidDiscoverServices
object:@{@"peripheral":peripheral,@"error":error?error:@""}];
//BabyLog(@">>>掃描到服務(wù):%@",peripheral.services);
if(error) {
BabyLog(@">>>didDiscoverServices for %@ with error: %@", peripheral.name, [error localizedDescription]);
}
//回叫block
if([currChannelblockOnDiscoverServices]) {
[currChannelblockOnDiscoverServices](peripheral,error);
}
//discover characteristics
if(needDiscoverCharacteristics) {
for(CBService*serviceinperipheral.services) {
[peripheraldiscoverCharacteristics:[currChannelbabyOptions].discoverWithCharacteristicsforService:service];
}
}
}
//發(fā)現(xiàn)服務(wù)的Characteristics
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverCharacteristicsForService:(CBService*)service error:(NSError*)error {
//發(fā)出通知
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidDiscoverCharacteristicsForService
object:@{@"peripheral":peripheral,@"service":service,@"error":error?error:@""}];
if(error) {
BabyLog(@"error didDiscoverCharacteristicsForService for %@ with error: %@", service.UUID, [error localizedDescription]);
//return;
}
//回叫block
if([currChannelblockOnDiscoverCharacteristics]) {
[currChannelblockOnDiscoverCharacteristics](peripheral,service,error);
}
//如果需要更新Characteristic的值
if(needReadValueForCharacteristic) {
for(CBCharacteristic*characteristicinservice.characteristics) {
//[peripheral readValueForCharacteristic:characteristic];
[peripheralsetNotifyValue:YESforCharacteristic:characteristic];
//判斷讀寫權(quán)限
//if (characteristic.properties & CBCharacteristicPropertyRead ) {
//[peripheral readValueForCharacteristic:characteristic];
//}
}
}
//如果搜索Characteristic的Descriptors
if(needDiscoverDescriptorsForCharacteristic) {
for(CBCharacteristic*characteristicinservice.characteristics) {
[peripheraldiscoverDescriptorsForCharacteristic:characteristic];
}
}
}
//讀取Characteristics的值
- (void)peripheral:(CBPeripheral*)peripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
//發(fā)出通知
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidUpdateValueForCharacteristic
object:@{@"peripheral":peripheral,@"characteristic":characteristic,@"error":error?error:@""}];
if(error) {
BabyLog(@"error didUpdateValueForCharacteristic %@ with error: %@", characteristic.UUID, [error localizedDescription]);
//return;
}
//查找字段訂閱
if([babySpeakernotifyCallback:characteristic]) {
[babySpeakernotifyCallback:characteristic](peripheral,characteristic,error);
return;
}
//回叫block
if([currChannelblockOnReadValueForCharacteristic]) {
[currChannelblockOnReadValueForCharacteristic](peripheral,characteristic,error);
}
}
//發(fā)現(xiàn)Characteristics的Descriptors
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
if(error) {
BabyLog(@"error Discovered DescriptorsForCharacteristic for %@ with error: %@", characteristic.UUID, [error localizedDescription]);
//return;
}
//回叫block
if([currChannelblockOnDiscoverDescriptorsForCharacteristic]) {
[currChannelblockOnDiscoverDescriptorsForCharacteristic](peripheral,characteristic,error);
}
//如果需要更新Characteristic的Descriptors
if(needReadValueForDescriptors) {
for(CBDescriptor*dincharacteristic.descriptors) {
[peripheralreadValueForDescriptor:d];
}
}
//執(zhí)行一次的方法
if(oneReadValueForDescriptors) {
for(CBDescriptor*dincharacteristic.descriptors) {
[peripheralreadValueForDescriptor:d];
}
oneReadValueForDescriptors=NO;
}
}
//讀取Characteristics的Descriptors的值
- (void)peripheral:(CBPeripheral*)peripheral didUpdateValueForDescriptor:(CBDescriptor*)descriptor error:(NSError*)error {
if(error) {
BabyLog(@"error didUpdateValueForDescriptorfor %@ with error: %@", descriptor.UUID, [error localizedDescription]);
//return;
}
//回叫block
if([currChannelblockOnReadValueForDescriptors]) {
[currChannelblockOnReadValueForDescriptors](peripheral,descriptor,error);
}
}
- (void)peripheral:(CBPeripheral*)peripheral didWriteValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidWriteValueForCharacteristicobject:@{@"characteristic":characteristic,@"error":error?error:@""}];
//BabyLog(@">>>didWriteValueForCharacteristic");
//BabyLog(@">>>uuid:%@,new value:%@",characteristic.UUID,characteristic.value);
if([currChannelblockOnDidWriteValueForCharacteristic]) {
[currChannelblockOnDidWriteValueForCharacteristic](characteristic,error);
}
}
- (void)peripheral:(CBPeripheral*)peripheral didWriteValueForDescriptor:(CBDescriptor*)descriptor error:(NSError*)error {
//BabyLog(@">>>didWriteValueForCharacteristic");
//BabyLog(@">>>uuid:%@,new value:%@",descriptor.UUID,descriptor.value);
if([currChannelblockOnDidWriteValueForDescriptor]) {
[currChannelblockOnDidWriteValueForDescriptor](descriptor,error);
}
}
#pragma mark--------------數(shù)據(jù)狀態(tài)改變-------------
//characteristic.isNotifying狀態(tài)改變
- (void)peripheral:(CBPeripheral*)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error {
[peripheralreadValueForCharacteristic:characteristic];
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidUpdateNotificationStateForCharacteristicobject:@{@"characteristic":characteristic,@"error":error?error:@""}];
BabyLog(@">>>didUpdateNotificationStateForCharacteristic");
BabyLog(@">>>uuid:%@,isNotifying:%@",characteristic.UUID,characteristic.isNotifying?@"isNotifying":@"Notifying");
if([currChannelblockOnDidUpdateNotificationStateForCharacteristic]) {
[currChannelblockOnDidUpdateNotificationStateForCharacteristic](characteristic,error);
}
}
- (void)peripheral:(CBPeripheral*)peripheral didDiscoverIncludedServicesForService:(CBService*)service error:(NSError*)error {
if([currChannelblockOnDidDiscoverIncludedServicesForService]) {
[currChannelblockOnDidDiscoverIncludedServicesForService](service,error);
}
}
# if__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
- (void)peripheralDidUpdateRSSI:(CBPeripheral*)peripheral error:(nullableNSError*)error {
[[NSNotificationCenterdefaultCenter]postNotificationName:BabyNotificationAtDidReadRSSIobject:@{@"peripheral":peripheral,@"RSSI":peripheral.RSSI,@"error":error?error:@""}];
BabyLog(@">>>peripheralDidUpdateRSSI -> RSSI:%@",peripheral.RSSI);
if([currChannelblockOnDidReadRSSI]) {
[currChannelblockOnDidReadRSSI](peripheral.RSSI,error);
}
}
#else
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
[[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidReadRSSI object:@{@"peripheral":peripheral,@"RSSI":RSSI,@"error":error?error:@""}];
BabyLog(@">>>peripheralDidUpdateRSSI -> RSSI:%@",RSSI);
if([currChannel blockOnDidReadRSSI]) {
[currChannel blockOnDidReadRSSI](RSSI,error);
}
}
#endif
- (void)peripheralDidUpdateName:(CBPeripheral*)peripheral {
if([currChannelblockOnDidUpdateName]) {
[currChannelblockOnDidUpdateName](peripheral);
}
}
- (void)peripheral:(CBPeripheral*)peripheral didModifyServices:(NSArray*)invalidatedServices {
if([currChannelblockOnDidModifyServices]) {
[currChannelblockOnDidModifyServices](peripheral,invalidatedServices);
}
}
/**
sometimes ever澳腹,sometimes never.相聚有時(shí)织盼,后會(huì)無期
this is center with peripheral's story
**/
//sometimes ever:添加斷開重連接的設(shè)備
-(void)sometimes_ever:(CBPeripheral*)peripheral {
if(![reConnectPeripheralscontainsObject:peripheral]) {
[reConnectPeripheralsaddObject:peripheral];
}
}
//sometimes never:刪除需要重連接的設(shè)備
-(void)sometimes_never:(CBPeripheral*)peripheral {
[reConnectPeripheralsremoveObject:peripheral];
}
#pragma mark -私有方法
#pragma mark -設(shè)備list管理
- (void)addDiscoverPeripheral:(CBPeripheral*)peripheral{
if(![discoverPeripheralscontainsObject:peripheral]) {
[discoverPeripheralsaddObject:peripheral];
}
}
- (void)addPeripheral:(CBPeripheral*)peripheral {
if(![connectedPeripheralscontainsObject:peripheral]) {
[connectedPeripheralsaddObject:peripheral];
}
}
- (void)deletePeripheral:(CBPeripheral*)peripheral{
[connectedPeripheralsremoveObject:peripheral];
}
- (CBPeripheral*)findConnectedPeripheral:(NSString*)peripheralName {
for(CBPeripheral*pinconnectedPeripherals) {
if([p.nameisEqualToString:peripheralName]) {
returnp;
}
}
returnnil;
}
- (NSArray*)findConnectedPeripherals{
returnconnectedPeripherals;
}
@end