在特征上设置通知会导致无效句柄错误

Sur*_*esh 57 macos bluetooth objective-c ios core-bluetooth

使用CoreBluetooth我想将数据从iPhone发送到Mac.为此,我编写了像iPhone一样的代码作为'Peripheral'和Mac作为'Central'.

它工作得很好,但有时会直接断开连接,然后连续连接和断开连接.

有时当它试图重新连接时,在中心它直接调用'didDisconnectPeripheral'委托方法.但有时它在'didUpdateNotificationStateForCharacteristic'中有错误"句柄无效".

我在网上提到了所有的链接.但我无法解决这个问题.我在iPhone中认为它存储了蓝牙缓存.

请提出解决方法如何解决"句柄无效"错误?

以下是一些重要的方法.

对于Peripheral,我写了如下代码.

在Appdelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.peripheral = [[PeripheralServerObject alloc] init];
self.peripheral.serviceUUID = [CBUUID UUIDWithString:@"4w24"];
return YES;
}
Run Code Online (Sandbox Code Playgroud)

在外围对象文件中:

//To Check Bluetooth State
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
    switch (peripheral.state) {
        case CBPeripheralManagerStatePoweredOn:
            [self enableService];
            break;
        case CBPeripheralManagerStatePoweredOff: {
            [self disableService];
            break;
        }
}

// To Add characteristics to Service
- (void)enableService
{
[self.peripheral removeAllServices];
 self.service = [[CBMutableService alloc]
                    initWithType:self.serviceUUID primary:YES];

self.authChar =
        [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"a86e"]
                                           properties:CBCharacteristicPropertyNotify
                                                value:nil
                                          permissions:CBAttributePermissionsReadable];


self.respChar =
        [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"a86f"]
                                           properties:CBCharacteristicPropertyWriteWithoutResponse
                                                value:nil
                                          permissions:CBAttributePermissionsWriteable];

self.service.characteristics = @[ self.authChar, self.respChar ];

    // Add the service to the peripheral manager.
    [self.peripheral addService:self.service];
}

//Peripheral Manager delegate method will be called after adding service.

- (void)peripheralManager:(CBPeripheralManager *)peripheral
            didAddService:(CBService *)service
                    error:(NSError *)error {

    [self startAdvertising];

}

//To disable service 
- (void)disableService
{
 [self.peripheral stopAdvertising];
 [self.peripheral removeAllServices];
}

//To enable a service again.
-(void)refreshService {
    [self disableService];
    [self enableService];
}


If central subscribes the characteristic, then the below peripheral delegate method will be called. In this I implemented code to send data

- (void)peripheralManager:(CBPeripheralManager *)peripheral
                  central:(CBCentral *)central
didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {

    self.dataTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                                      target:self
                                                    selector:@selector(sendData)
                                                    userInfo:nil
                                                     repeats:YES];
}

- (void)sendData
{
Here I am sending data like [Apple's BTLE Example Code][1]  
}


//If unsubscribed then I am invalidating timer and refreshing service

- (void)peripheralManager:(CBPeripheralManager *)peripheral
                  central:(CBCentral *)central
didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic {

    if (self.dataTimer)
        [self.dataTimer invalidate];
    [self refreshService];

}
Run Code Online (Sandbox Code Playgroud)

对于Mac,我写了一个外围委托方法.

//I enables the notification for "a860" Characteristic.

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

     CBUUID * authUUID = [CBUUID UUIDWithString:@"a86e"];
       for (CBCharacteristic *characteristic in service.characteristics) {

        if ([characteristic.UUID isEqual:authUUID]) {
         }
        [self.connectedPeripheral setNotifyValue:YES
                                   forCharacteristic:characteristic];
         }
}

-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
   if (error) {
   Here I am getting error sometimes "The handle is invalid".
    }
}
Run Code Online (Sandbox Code Playgroud)

Kád*_*ádi 3

我最近遇到了同样的问题。我发现的唯一解决方案是重新启动蓝牙(关闭蓝牙然后重新打开)。

就我而言,蓝牙设备的更改(在 DFU 模式下重新启动)总是导致此问题,因此我最终向用户显示重新启动蓝牙的警报。监听 的centralManagerDidUpdateState:Powered Off 和 Powered back On 状态事件以确定是否重新启动已完成。