在无效句柄误差特性结果设置通知 [英] Setting Notifications on Characteristic results in Invalid Handle error
问题描述
使用CoreBluetooth我想从iPhone发送数据到Mac。为此,我写了code像iPhone一样外围和Mac的中央。
它可以完美,但有时它直接断开,然后不断地连接和断开连接。
当它试图重新连接有些时候,在中央直接称之为didDisconnectPeripheral委托方法。但有些时候,它的主要错误句柄无效,在didUpdateNotificationStateForCharacteristic。
我提到净的所有环节。但我不能够解决这个问题。我想在iPhone它被存储蓝牙高速缓存。
请提出一个解决方案如何解决的句柄无效的错误?
下面是一些重要的方法。
有关外设我写了code像下面。
在AppDelegate中:
- (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions
{
self.peripheral = [[PeripheralServerObject的alloc]初始化];
self.peripheral.serviceUUID = [CBUUID UUIDWithString:@4w24];
返回YES;
}
在外围对象文件:
//要检查蓝牙状态
- (无效)peripheralManagerDidUpdateState:(CBPeripheralManager *){周边
开关(peripheral.state){
案例CBPeripheralManagerStatePoweredOn:
[个体经营enableService]
打破;
案例CBPeripheralManagerStatePoweredOff:{
[个体经营disableService]
打破;
}
}//要添加特性,以服务
- (无效)enableService
{
[self.peripheral removeAllServices]
self.service = [[CBMutableService页头]
initWithType:self.serviceUUID小学:YES];self.authChar =
[CBMutableCharacteristic页头] initWithType:[CBUUID UUIDWithString:@a86e]
属性:CBCharacteristicPropertyNotify
值:无
权限:CBAttributePermissionsReadable];
self.respChar =
[CBMutableCharacteristic页头] initWithType:[CBUUID UUIDWithString:@a86f]
属性:CBCharacteristicPropertyWriteWithoutResponse
值:无
权限:CBAttributePermissionsWriteable];self.service.characteristics = @ [self.authChar,self.respChar] //服务添加到外围经理。
[self.peripheral addService:self.service];
}//外围器件管理的委托方法将加入服务后调用。 - (无效)peripheralManager:(CBPeripheralManager *)外设
didAddService:(CBService *)服务
错误:(NSError *)错误{ [个体经营startAdvertising]}//禁用服务
- (无效)disableService
{
[self.peripheral stopAdvertising]
[self.peripheral removeAllServices]
}//要再次启用服务。
- (无效){refreshService
[个体经营disableService]
[个体经营enableService]
}
如果中央订阅特性,那么下面的外围委托方法将被调用。在此我实现了code发送数据 - (无效)peripheralManager:(CBPeripheralManager *)外设
中央:(CBCentral *)中央
didSubscribeToCharacteristic:(CBCharacteristic *){特点 self.dataTimer = [的NSTimer scheduledTimerWithTimeInterval:10.0
目标:自我
选择:@selector(送出数据)
用户信息:无
重复:YES];
} - (无效)送出数据
{
在这里,我喜欢发送数据[苹果BTLE举例code] [1]
}
//如果退订那么我无效定时器和清凉服务 - (无效)peripheralManager:(CBPeripheralManager *)外设
中央:(CBCentral *)中央
didUnsubscribeFromCharacteristic:(CBCharacteristic *){特点 如果(self.dataTimer)
[self.dataTimer无效]
[个体经营refreshService]}
对于Mac我写了一个外围委托方法。
//我能够为A860通知特性。 - (无效)外围:(CBPeripheral *)外设
didDiscoverCharacteristicsForService:(CBService *)服务
错误:(NSError *)错误{ CBUUID * authUUID = [CBUUID UUIDWithString:@a86e];
对于(CBCharacteristic *在service.characteristics特性){ 如果([characteristic.UUID的isEqual:authUUID]){
}
[self.connectedPeripheral setNotifyValue:YES
forCharacteristic:特点]
}
} - (无效)外围:(CBPeripheral *)周边didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)特性误差:(NSError *)错误{
如果(错误){
在这里,我有时会得到错误句柄是无效的。
}
}
最近,我遇到了同样的问题。
我发现的唯一的解决办法是重新启动蓝牙(关闭蓝牙,并重新打开它)。
在我的情况下,它是在蓝牙设备中的变化(DFU模式重新启动它)总是引起这个问题,所以我最终显示警报用户重新启动蓝牙。听了 centralManagerDidUpdateState:
的断电,并且比通电后状态的事件,以确定是否重启已完成
Using CoreBluetooth I want to send data from iPhone to Mac. For this I wrote code like iPhone as 'Peripheral' and Mac as 'Central'.
It works perfectly, but sometimes it disconnects directly and then it continuously connects and disconnects.
Some times when it is trying to reconnect, In Central it directly calls 'didDisconnectPeripheral' delegate method. But some times it has error "The handle is invalid" in 'didUpdateNotificationStateForCharacteristic'.
I referred all the links in net. But I am not able to solve this problem. I thought in iPhone it was storing Bluetooth cache.
Please suggest a solution how to solve "The handle is invalid" error?
Below are some of the important methods.
For Peripheral I wrote Code like below.
In Appdelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.peripheral = [[PeripheralServerObject alloc] init];
self.peripheral.serviceUUID = [CBUUID UUIDWithString:@"4w24"];
return YES;
}
In Peripheral Object File:
//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];
}
For Mac I wrote a peripheral delegate methods.
//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".
}
}
I recently run into the same problem. The only solution I found was to restart the bluetooth (turn bluetooth off and turn it back on).
In my case, it was a change in the bluetooth device (restarting it in DFU mode) that always caused this problem, so I ended up showing an alert to the user to restart the bluetooth. Listened for centralManagerDidUpdateState:
's Powered Off and than Powered back On state event to determine if the restart was done.
这篇关于在无效句柄误差特性结果设置通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!