蓝牙中央管理器无法连接到新服务

时间:2014-07-22 19:16:20

标签: ios7 core-bluetooth cbcentralmanager cbperipheral

我正在尝试创建一个应用程序,其中两个不同的设备可以使用相同的UIControlView相互通信,使用iBeacon来回发送通知,并使用bluetoothle来回发送数据。

我首先派出一个邻近的iBeacon来询问该地区的人是否想要玩游戏。

如果收到通知的人点击了" OK"按钮他们宣传蓝牙特征,其价值是原始人正在收听的唯一用户ID和服务,然后是iBeacon通知,提醒他们想要播放的原始人。

完成此操作后,应用程序将停止外设广告,并根据自己的用户ID和部分UUID启动centralManager扫描不同的服务UUID。

我使用位置管理器来获取邀请通知。然后当" OK"我点按了按钮

    -(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{

        if (AlertView) {
         NSLog(@"idenify:%@",identifierString);
            if ([identifierString isEqualToString:@"com.checkers.bluetooth"]) {
                    if (buttonIndex == 0) {
                        [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 
//here I clear the CentralManager so a new service can be looked for
centralManager = nil;
//here the peripheralManager sends the unique code
[self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:sendUUID]] }]; recieve.frame = CGRectMake(147,55,20,20);  } completion:^ (BOOL completed) {
//here the peripheralManager sends the proximity notification
 NSUUID * uid = [[NSUUID alloc] initWithUUIDString:@"55DCE9F4-7942-4B9D-B914-50426D6CC976"];
                        self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uid identifier:@"com.checkersok.bluetooth"];
                        [self configureTransmitter];
                           [self.peripheralManager startAdvertising:self.peripheralData];inviteCheckers.hidden = YES; [self.recieve setBackgroundImage:[UIImage imageNamed:@"accept.png"] forState:UIControlStateNormal];[UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionCurveEaseIn animations:^{ movementImage.frame = CGRectMake(300,292,17,17);
                        } completion:^ (BOOL completed) {
//here the peripheral manager is stopped
 [self.peripheralManager stopAdvertising];
                            NSString *startString2 = userId.text;
                            NSString *endString2 = [startString2 stringByAppendingString:@"-2C29-40E0-8E1E-1D7A9E5D0964"];
                            recieverUUID =endString2;
                            self.userUUIDString = @"YES";
                            NSLog(@"userUUIDString2;%@",userUUIDString);
                            NSLog(@"recieverUUID7;%@",recieverUUID);

//here the centralManager is allocated again and starts scanning for the new service UUID
 centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
                            [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:recieverUUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];}         ];}         ];


                        NSLog(@"userUUIDString:%@",userUUIDString);

                        [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
                        [[UIApplication sharedApplication] cancelAllLocalNotifications];
                }

当发件人收到有人愿意播放的通知时,我将peripheralManager设置为nil,这样我就可以将其设置为其他内容。

当原始人(发件人)点击" OK"按钮我重新分配外围设备管理器,并根据第二个用户ID和UUID的同一端开始广播服务UUID。这是sendUUID。

else if ([identifierString isEqualToString:@"com.checkersok.bluetooth"]) {
            if (buttonIndex == 0) {

                [timer4 invalidate];
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
               [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:sendUUID]] }];
                 movementImage.frame = CGRectMake(283,292,17,17);  } completion:^ (BOOL completed) {
                    }         ];
                [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
                [[UIApplication sharedApplication] cancelAllLocalNotifications];
                self.userUUIDString = userField.text;

            }

这就是问题所在。 我知道正在调用peripheralManager并且正在发送数据,并且CentralManager正在通过日志进行连接。

这是发件人日志。

2014-07-23 16:34:53.215 BTLE Games[344:60b] self.peripheralManager powered on.
2014-07-23 16:34:53.218 BTLE Games[344:60b] transfer char1:(
    "<CBMutableCharacteristic: 0x17db26f0 UUID = C63CBCC0-6DE3-48D5-BB59-2BEFB9F3AE01, Value = (null), Properties = 0x10, Permissions = 0x1, Descriptors = (null), SubscribedCentrals = (\n)>"
)
2014-07-23 16:34:53.220 BTLE Games[344:60b] serviceUUID;CDEF5678-2C29-40E0-8E1E-1D7A9E5D0964
2014-07-23 16:34:54.030 BTLE Games[344:60b] Central subscribed to characteristic
2014-07-23 16:34:54.032 BTLE Games[344:60b] datatosend2:<41424344 31323334>
2014-07-23 16:34:54.033 BTLE Games[344:60b] stringOne:ABCD1234

这是收件人日志。

2014-07-23 17:22:39.723 BTLE Games[358:60b] CoreBluetooth[WARNING] <CBCentralManager: 0x170247ec0> is disabling duplicate filtering, but is using the default queue (main thread) for delegate events
2014-07-23 17:22:39.724 BTLE Games[358:60b] CoreBluetooth[API MISUSE] <CBCentralManager: 0x170247ec0> can only accept commands while in the powered on state
2014-07-23 17:22:39.743 BTLE Games[358:60b] Discovered iPhone at -50
2014-07-23 17:22:39.744 BTLE Games[358:60b] Connecting to peripheral <CBPeripheral: 0x1782a0480 identifier = DFDBFE05-B652-D5AD-ABA7-10162669E837, Name = "iPhone", state = disconnected>
2014-07-23 17:22:39.745 BTLE Games[358:60b] Discovered iPhone at -50
2014-07-23 17:22:39.747 BTLE Games[358:60b] Peripheral Connected
2014-07-23 17:22:39.747 BTLE Games[358:60b] Scanning stopped
2014-07-23 17:22:39.748 BTLE Games[358:60b] recieverUUIDP;CDEF5678-2C29-40E0-8E1E-1D7A9E5D0964
2014-07-23 17:22:39.965 BTLE Games[358:60b] discovered characteristic13 C63CBCC0-6DE3-48D5-BB59-2BEFB9F3AE01
2014-07-23 17:22:39.966 BTLE Games[358:60b] Found Notify Characteristic31 <CBCharacteristic: 0x17808f730 UUID = C63CBCC0-6DE3-48D5-BB59-2BEFB9F3AE01, Value = (null), Properties = 0x10, Notifying = NO, Broadcasting = NO>
2014-07-23 17:22:39.967 BTLE Games[358:60b] value1:(null)
2014-07-23 17:22:40.009 BTLE Games[358:60b] CoreBluetooth[WARNING] <CBCentralManager: 0x170247ec0> is disabling duplicate filtering, but is using the default queue (main thread) for delegate events
2014-07-23 17:22:40.010 BTLE Games[358:60b] 

如您所见,peripheralManager正在做广告,而centralManager正在扫描。 中央找到外围设备并连接但停止。通常外围设备继续做广告,并且中央必须在获取数据之前连接和断开连接一段时间。这是我不知道如何开始工作的原因,因为我使用相同的代码来发送和接收这些数据,而我使用从第二个人发送到第一个人的数据。

我尝试了几种不同的方法来实现这一目标,但不能。 任何人都可以告诉我如何让它发挥作用吗?

0 个答案:

没有答案