iOS Core Bluetooth未请求配对

12
在我的最近的项目中,我需要与一种硬件(低功耗蓝牙)进行通信。我已经实现了所有代理方法的代码。我能够连接设备和硬件,但是我没有收到配对警报(附带的屏幕截图)。为什么它不要求配对?谢谢。
  #import "BTWCentralConnectionManager.h"

    @implementation BTWCentralConnectionManager

    @synthesize cbcManager;

    @synthesize discoveredPeripheral;

    @synthesize findMeServiceCharacteristic;

    @synthesize findMeService;

    @synthesize delegate=_delegate;

    static NSString *kFindMeServiceUUID=@"1802";

    static NSString *kFindMeCharacteristicUUID=@"2A06";

    static BTWCentralConnectionManager* connectionManager = nil;

    +(BTWCentralConnectionManager *)sharedConnectionManager{

    @synchronized(self)

    {

        if (!connectionManager){

            connectionManager=[[self alloc] init];


        }

        return connectionManager;

    }

    return nil;

}


    -(void)findMe {

    Byte code=0x02;

    if(self.discoveredPeripheral){

        [self.discoveredPeripheral writeValue:[NSData dataWithBytes:&code length:1] forCharacteristic:self.findMeServiceCharacteristic type:CBCharacteristicWriteWithoutResponse];


    }else{

        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:@"Invalid Charactersitcs" delegate:nil cancelButtonTitle:nil     otherButtonTitles:@"OK", nil];

        [alertView show];

        alertView=nil;

    }
}

-(void)searchForDevices{

    self.cbcManager=[[CBCentralManager alloc] initWithDelegate:self queue:nil];

}

    -(void)connect {

    NSDictionary* connectOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey];


    [self.cbcManager connectPeripheral:self.discoveredPeripheral options:connectOptions];

}

    -(void)disconnect{

    [self cleanup];

}


- (void)centralManagerDidUpdateState:(CBCentralManager *)central {

    switch (central.state) {

        case CBCentralManagerStatePoweredOn:{

            [self.cbcManager scanForPeripheralsWithServices:@[ [CBUUID UUIDWithString:kFindMeServiceUUID] ] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @NO }];

        }

            break;

            // Scans for any peripheral

        default:{

            UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:@"Cental Manager did change state" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

            [alertView show];

            alertView=nil;

        }

            break;
    }

}


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

    // Stops scanning for peripheral

    [self.cbcManager stopScan];


    if (self.discoveredPeripheral != peripheral) {

        self.discoveredPeripheral = peripheral;

        [self.delegate didDeviceDiscoverd:self.discoveredPeripheral.name];

    }

}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{

    [self.delegate didDeviceConnectionFailed:error];

    [self cleanup];
}


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{

    [self.delegate didDeviceConnected];

    [self.discoveredPeripheral setDelegate:self];

    [self.discoveredPeripheral discoverServices:@[[CBUUID UUIDWithString:kFindMeServiceUUID]]];

}


- (void)peripheral:(CBPeripheral *)aPeripheral didDiscoverServices:(NSError *)error {

    if (error) {

        NSString *strMsg=[NSString stringWithFormat:@"didDiscoverServices: %@",  error];

        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg 
delegate:nil cancelButtonTitle:nil     otherButtonTitles:@"OK", nil];

        [alertView show];

        alertView=nil;

        [self cleanup];

        return;

    }

    for (CBService *service in aPeripheral.services) {

        if ([service.UUID isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]) {

            self.findMeService=service;

            [self.discoveredPeripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kFindMeCharacteristicUUID]] forService:self.findMeService];

        }

    }

}

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{

    if(error){

        NSString *strMsg=[NSString stringWithFormat:@"didDiscoverCharacteristicsForService: %@",  error];

        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil     otherButtonTitles:@"OK", nil];

        [alertView show];

        alertView=nil;

    }

    for(CBCharacteristic *character in [service characteristics])
    {

        if([[service UUID] isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]] &&
           [[character UUID] isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]])
        {

            NSString *strMsg=[NSString stringWithFormat:@"didDiscoverCharacteristicsForService: %@",  character];

            UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil     otherButtonTitles:@"OK", nil];

            [alertView show];

            alertView=nil;

            self.findMeServiceCharacteristic = character;

        }
    }
}


- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

{

    NSString *strMsg=[NSString stringWithFormat:@"Did update value for characteristic %@, new value: %@, error: %@", characteristic, [characteristic value], error];

    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil     otherButtonTitles:@"OK", nil];

    [alertView show];

    alertView=nil;


}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

    if (error) {

        NSLog(@"Error changing notification state: %@", error.localizedDescription);

    }


    // Exits if it's not the transfer characteristic

    if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]]) {

        return;

    }


    NSString *strMsg=[NSString stringWithFormat:@"didUpdateNotificationStateForCharacteristic %@, reason: %@", characteristic, error];

    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil     otherButtonTitles:@"OK", nil];

    [alertView show];


    alertView=nil;

}

- (void) peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

{

    if (error)

    {

        NSString *strMsg=[NSString stringWithFormat:@"Failed to write value for characteristic %@, reason: %@", characteristic, error];

        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil     otherButtonTitles:@"OK", nil];

        [alertView show];

        alertView=nil;

    }
    else
    {
        NSString *strMsg=[NSString stringWithFormat:@"Did write value for characterstic %@, new value: %@", characteristic, [characteristic value]];

        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil     otherButtonTitles:@"OK", nil];

        [alertView show];

        alertView=nil;

    }
}

- (void)cleanup

{

    if (!self.discoveredPeripheral.isConnected) {

        return;

    }

    if (self.discoveredPeripheral.services != nil) {

        for (CBService *service in self.discoveredPeripheral.services) {

            if (service.characteristics != nil) {

                for (CBCharacteristic *characteristic in service.characteristics) {

                    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]) {

                        if (characteristic.isNotifying) {

                            [self.discoveredPeripheral setNotifyValue:NO forCharacteristic:characteristic];

                            return;

                        }

                    }

                }

            }
        }
    }
    [self.cbcManager cancelPeripheralConnection:self.discoveredPeripheral];
    [self.delegate didDeviceDisconnected];
}
@end
`

没有一些源代码,很难帮助你。 - Stanimirovv
谢谢Bloodcount,请检查更新后的问题。 - Mangesh
你尝试过使用iOS6设备吗? - Larme
我正在使用iOS 6的iPhone 5。 - Mangesh
截图在哪里? - Mrug
1个回答

22

如果我理解你的问题正确,你可以成功向某个特征写入一个值,但是你没有收到配对请求。

配对是由外围设备触发的。这意味着外围设备必须拒绝中央设备对某个特征的读取或写入请求。当出现“未经授权的身份验证”错误时,你的中央设备会尝试与外围设备配对并显示配对提示弹窗,所有这些都是通过Core Bluetooth自动完成的。你需要做的唯一一件事情就是在外围设备中更改特征选项和权限。以下是苹果示例代码触发配对的方法:

emailCharacteristic = [[CBMutableCharacteristic alloc]
    initWithType:emailCharacteristicUUID
    properties:CBCharacteristicPropertyRead
    | CBCharacteristicPropertyNotifyEncryptionRequired
    value:nil permissions:CBAttributePermissionsReadEncryptionRequired];

来源:CoreBluetooth_概念 iOS7 预览

还要查看WWDC 2012高级核心蓝牙视频,在28分钟处解释了配对的概念。


1
这是视频链接:http://adcdownload.apple.com//videos/wwdc_2012__hd/session_705__advanced_core_bluetooth.mov - ThomasW
@ThomasW,苹果的视频现在已经消失了吗?我找不到它。我在想是不是我的问题... - huggie
我想做的是相反的。我被告知它不应该触发配对警报,但现在它确实触发了。这是仅由设备决定的吗? - huggie
1
@huggie 看起来视频消失了,很奇怪。 - ThomasW

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接