iOS的蓝牙核心不要求对 [英] iOS Core Bluetooth Not asking for Pair

查看:146
本文介绍了iOS的蓝牙核心不要求对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最近的项目,我需要进行通信的硬件(蓝牙低耗能)。我已经实现所有委托方法code。我能够连接硬件和设备,但我没有得到配对警报(附截图)。为什么不被要求配对?谢谢。

 #进口BTWCentralConnectionManager.h    @implementation BTWCentralConnectionManager    @synthesize cbcManager;    @synthesize discoveredPeripheral;    @synthesize findMeServiceCharacteristic;    @synthesize findMeService;    @synthesize委托= _delegate;    静态的NSString * kFindMeServiceUUID = @1802;    静态的NSString * kFindMeCharacteristicUUID = @2A06    静态BTWCentralConnectionManager *的ConnectionManager =零;    +(BTWCentralConnectionManager *)sharedConnectionManager {    @synchronized(个体经营)    {        如果(!的ConnectionManager){            的ConnectionManager = [[自我的alloc]初始化];
        }        返回的ConnectionManager;    }    回零;}
     - (无效){findMe    字节code = 0×02;    如果(self.discoveredPeripheral){        [self.discoveredPeripheral writeValue:[NSData的dataWithBytes:放; code长度:1] forCharacteristic:self.findMeServiceCharacteristic类型:CBCharacteristicWriteWithoutResponse];
    }其他{        UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@测试消息:@无效Charactersitcs代表:无cancelButtonTitle:无otherButtonTitles:@OK,零]        [alertView秀]        alertView =零;    }
} - (无效){searchForDevices    self.cbcManager = [[CBCentralManager页头] initWithDelegate:自队列:无];}     - (无效)连接{    *的NSDictionary = connectOptions [NSDictionary的dictionaryWithObject:[NSNumber的numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey];
    [self.cbcManager connectPeripheral:self.discoveredPeripheral选项:connectOptions];}     - (无效){断开    [自我清理]}
- (无效)centralManagerDidUpdateState:(CBCentralManager *){中心    开关(central.state){        案例CBCentralManagerStatePoweredOn:{            [self.cbcManager scanForPeripheralsWithServices:@ [CBUUID UUIDWithString:kFindMeServiceUUID]选项:@ {CBCentralManagerScanOptionAllowDuplicatesKey:@NO}];        }            打破;            //扫描任何外围设备        默认:{            UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@测试消息:@百磅经理确实改变国家代表:无cancelButtonTitle:无otherButtonTitles:@OK,零]            [alertView秀]            alertView =零;        }            打破;
    }}
- (无效)centralManager:(CBCentralManager *)中央didDiscoverPeripheral:(CBPeripheral *)周边advertisementData:(NSDictionary的*)advertisementData RSSI(NSNumber的*){RSSI    //停止扫描周边    [self.cbcManager stopScan]
    如果(self.discoveredPeripheral!=外设){        self.discoveredPeripheral =外设;        [self.delegate didDeviceDiscoverd:self.discoveredPeripheral.name];    }} - (无效)centralManager:(CBCentralManager *)中央didFailToConnectPeripheral:(CBPeripheral *)外设错误:(NSError *)错误
{    [self.delegate didDeviceConnectionFailed:错误]    [自我清理]
}
- (无效)centralManager:(CBCentralManager *)中央didConnectPeripheral:(CBPeripheral *)外设
{    [self.delegate didDeviceConnected]    [self.discoveredPeripheral setDelegate:个体经营];    [self.discoveredPeripheral discoverServices:@ [[CBUUID UUIDWithString:kFindMeServiceUUID]]];}
- (无效)外围:(CBPeripheral *)aPeripheral didDiscoverServices:(NSError *)错误{    如果(错误){        * NSString的strMsg = [的NSString stringWithFormat:@didDiscoverServices:%@,错误]        UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@测试消息:strMsg
代表:无cancelButtonTitle:无otherButtonTitles:@OK,零]        [alertView秀]        alertView =零;        [自我清理]        返回;    }    对(在aPeripheral.services CBService *服务){        如果([service.UUID的isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]){            self.findMeService =服务;            [self.discoveredPeripheral discoverCharacteristics:@ [CBUUID UUIDWithString:kFindMeCharacteristicUUID] forService:self.findMeService];        }    }} - (无效)外围:(CBPeripheral *)周边didDiscoverCharacteristicsForService:(CBService *)服务错误:(NSError *)错误
{    如果(错误){        * NSString的strMsg = [的NSString stringWithFormat:@didDiscoverCharacteristicsForService:%@,错误]        UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@测试消息:strMsg代表:无cancelButtonTitle:无otherButtonTitles:@OK,零]        [alertView秀]        alertView =零;    }    对于(CBCharacteristic *字符[服务特色])
    {        如果([服务UUID]的isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]&放大器;&安培;
           [人物UUID]的isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]])
        {            * NSString的strMsg = [的NSString stringWithFormat:@didDiscoverCharacteristicsForService:%@字符]            UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@测试消息:strMsg代表:无cancelButtonTitle:无otherButtonTitles:@OK,零]            [alertView秀]            alertView =零;            self.findMeServiceCharacteristic =人物;        }
    }
}
- (无效)外围:(CBPeripheral *)周边didUpdateValueForCharacteristic:(CBCharacteristic *)特性误差:(NSError *)错误{    * NSString的strMsg = [的NSString stringWithFormat:@难道为特征%@,新的值更新值:%@,错误:%@,特点,[特征值],错误]    UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@测试消息:strMsg代表:无cancelButtonTitle:无otherButtonTitles:@OK,零]    [alertView秀]    alertView =零;
} - (无效)外围:(CBPeripheral *)周边didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)特性误差:(NSError *)错误{    如果(错误){        的NSLog(@错误更改通知状态:%@,error.localizedDescription);    }
    //退出,如果它不是传输特性    如果([characteristic.UUID的isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]!){        返回;    }
    * NSString的strMsg = [的NSString stringWithFormat:@didUpdateNotificationStateForCharacteristic%@,原因是:%@,特征,错误]    UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@测试消息:strMsg代表:无cancelButtonTitle:无otherButtonTitles:@OK,零]    [alertView秀]
    alertView =零;} - (无效)外围:(CBPeripheral *)周边didWriteValueForCharacteristic:(CBCharacteristic *)特性误差:(NSError *)错误{    如果(错误)    {        * NSString的strMsg = [的NSString stringWithFormat:@无法写特征%@,原因值:%@,特征,错误]        UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@测试消息:strMsg代表:无cancelButtonTitle:无otherButtonTitles:@OK,零]        [alertView秀]        alertView =零;    }
    其他
    {
        * NSString的strMsg = [的NSString stringWithFormat:@难道为characterstic%@,新的值写入值:%@,特点,[特征值]];        UIAlertView中* alertView = [[UIAlertView中页头] initWithTitle:@测试消息:strMsg代表:无cancelButtonTitle:无otherButtonTitles:@OK,零]        [alertView秀]        alertView =零;    }
} - (无效)清理{    如果(!self.discoveredPeripheral.isConnected){        返回;    }    如果(self.discoveredPeripheral.services!=无){        对(在self.discoveredPeripheral.services CBService *服务){            如果(service.characteristics!=无){                对于(CBCharacteristic *在service.characteristics特性){                    如果([characteristic.UUID的isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]){                        如果(characteristic.isNotifying){                            [self.discoveredPeripheral setNotifyValue:NO forCharacteristic:特点]                            返回;                        }                    }                }            }
        }
    }
    [self.cbcManager cancelPeripheralConnection:self.discoveredPeripheral];
    [self.delegate didDeviceDisconnected]
}
@结束
`


解决方案

如果我理解你的权利,你可以成功地写入值的特性,但你没有得到的配对请求。

配对是由外围触发。这意味着外围有权拒绝写或读您的中央的要求,特点。您的中心得到拒绝非法认证,然后尝试配对的外设和显示配对警报弹出你正在等待。这都是由核心蓝牙自动完成。你需要做的唯一的事情就是改变特性选项和权限,在你的周围。这是苹果样本code触发配对:

  emailCharacteristic = [[CBMutableCharacteristic页头]
    initWithType:emailCharacteristicUUID
    属性:CBCharacteristicPropertyRead
    | CBCharacteristicPropertyNotifyEncryptionRequired
    值:无权限:CBAttributePermissionsReadEncryptionRequired];

来源: CoreBluetooth _concepts iOS7 preVIEW

还检查了WWDC 2012先进的核心蓝牙视频。在约2/3他们解释配对的概念。

In My recent project, I need to communicate a Hardware (Bluetooth Low energy).I have implement all the delegate methods code. I am able to Connect hardware and device, But I am not getting pairing alert (Attached screen shot). Why not it is asking for pairing? Thank you.

  #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
`

解决方案

If I understand you right you can write a value successfully to a characteristic but you don't get a pairing request.

The pairing is triggered by the peripheral. Meaning the peripheral has to refuse the write or read request of your central to a characteristic. Your central gets the refusal "unauthorized authentication" and then tries to pair with the peripheral and showing the pairing alert pop up you are waiting for. This is all done by core bluetooth automatically. The only thing you need to do is change the characteristics options and permissions in your peripheral. This is the apple sample code to trigger a pairing:

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

source: CoreBluetooth_concepts iOS7 preview

Also check out the WWDC 2012 advanced core bluetooth video. at approx. 2/3 they explain the concept of pairing.

这篇关于iOS的蓝牙核心不要求对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆