Establecer notificaciones de resultados característicos en error de manejo no válido
Usando CoreBluetooth quiero enviar datos desde iPhone a Mac. Para esto escribí código como iPhone como 'Periférico' y Mac como 'Central'.
Funciona perfectamente, pero a veces se desconecta directamente y luego se conecta y desconecta continuamente.
Algunas veces, cuando intenta reconectarse, In Central llama directamente al método delegado 'didDisconnectPeripheral'. Pero algunas veces tiene el error "El identificador no es válido" en 'didUpdateNotificationStateForCharacteristic'.
Remití todos los enlaces en la red. Pero no puedo resolver este problema. Pensé que en iPhone estaba almacenando caché Bluetooth.
Sugiera una solución para resolver el error "El identificador no es válido".
A continuación se presentan algunos de los métodos importantes.
Para Periférico, escribí Código como a continuación.
En Appdelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.peripheral = [[PeripheralServerObject alloc] init];
self.peripheral.serviceUUID = [CBUUID UUIDWithString:@"4w24"];
return YES;
}
En archivo de objetos periféricos:
//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];
}
Para Mac escribí un método delegado periférico.
//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".
}
}