【问题标题】:iOS BLE peripherals keep disconnecting immediately after discoverServices is called调用 discoverServices 后,iOS BLE 外围设备会立即断开连接
【发布时间】:2014-02-01 17:30:34
【问题描述】:

在为 iOS 开发 BLE 应用程序时,我在调用 discoverServices 后立即断开连接。我正在使用 4 个完全相同的 BLE 设备 (OEM) 进行测试,并且我一直在完全相同的两个设备上断开连接。每次。我知道这些设备没问题,因为我还在 Android 上构建了相同的应用程序,并且使用相同的设备,所有 4 个都保持连接。这是使用 Titanium,但这里的所有内容都是在 iOS 中实现的。这是相关的 iOS 代码:

- (void)centralManagerDidUpdateState:(CBCentralManager *)central

    {
TiLogMessage(@"[INFO] ===== %@ - centralManagerDidUpdateState: entry",self);

NSString *state = nil;

switch (central.state) {
    case CBCentralManagerStatePoweredOn:
        state = @"CentralManagerStatePoweredOn";
        break;

    case CBCentralManagerStateUnknown:
        state = @"CentralManagerStateUnknown";
        break;

    case CBCentralManagerStateResetting:
        state = @"CentralManagerStateResetting";
        break;

    case CBCentralManagerStateUnsupported:
        state = @"CentralManagerStateUnsupported";
        break;

    case CBCentralManagerStateUnauthorized:
        state = @"CentralManagerStateUnauthorized";
        break;

    case CBCentralManagerStatePoweredOff:
        state = @"CentralManagerStatePoweredOff";
        break;

    default:
        TiLogMessage(@"[INFO] ===== %@ - centralManagerDidUpdateState default -> break",self);
        break;
}

TiLogMessage(@"[INFO] ===== %@ - centralManagerDidUpdateState state changed to: %@",self, state);

NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:state, @"state", nil];

if ([self _hasListeners:@"centralManagerStateChange"]) {
    [self fireEvent:@"centralManagerStateChange" withObject: event];
}

TiLogMessage(@"[INFO] ===== %@ - centralManagerDidUpdateState: exit",self);

}

- (void) connectPeripheral:(id)args
    {
TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - entry",self);

NSString* uuid = [[[args objectAtIndex:0] objectForKey:@"peripheral"] objectForKey: @"UUID"];

for (CBPeripheral *peripheral in self.discoveredPeripherals){
    if ([[peripheral.identifier UUIDString] isEqualToString:uuid]){
        TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - device is %@",self, peripheral);
        TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - attempting to connect to %@",self, peripheral.name);

        if (self.connectedPeripherals){
            TiLogMessage(@"[INFO] ===== %@ : connectedPeripheral - connectedPeripherals is ok...",self);

            if (![self.connectedPeripherals containsObject:peripheral]){
                TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - device is not connected. Connecting.",self);

                if (centralManager.state == CBCentralManagerStatePoweredOn){
                    [centralManager connectPeripheral:peripheral options:nil];
                }
            }
            else{
                TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - device is already connected. %@",self,peripheral);
            }
        }

    }
}

TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - exit",self);

}

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

    {
TiLogMessage(@"[INFO] ===== %@ : didConnectPeripheral - entry",self);

if ( [manuallyDisconnectedPeripherals containsObject:[peripheral.identifier UUIDString]] ){
    [manuallyDisconnectedPeripherals removeObject:[peripheral.identifier UUIDString]];
}

if (![self.connectedPeripherals containsObject:peripheral]){

    [peripheral setDelegate:self];
    [self.connectedPeripherals addObject: peripheral];

    NSNumber *RSSI = 0;
    if (peripheral.RSSI != nil){
        RSSI = peripheral.RSSI;
    }

    NSDictionary *peripheralObj = [NSDictionary dictionaryWithObjectsAndKeys: [peripheral.identifier UUIDString], @"UUID",
                               peripheral.name, @"name",[NSNumber numberWithInteger:1], @"isConnected", RSSI, @"RSSI", nil];

    NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys: peripheralObj, @"peripheral",
                       [peripheral.identifier UUIDString], @"UUID",nil];

    if ([self _hasListeners:@"didConnectPeripheral"]) {
        [self fireEvent:@"didConnectPeripheral" withObject: event];
    }
}

TiLogMessage(@"[INFO] ===== %@ : didConnectPeripheral - exit",self);

}

- (void) discoverServices:(id)args
    {
TiLogMessage(@"[INFO] ===== %@ : discoverServices - entry",self);

ENSURE_SINGLE_ARG(args,NSDictionary);
NSString *peripheralUUID = [[args objectForKey:@"peripheral"] objectForKey:@"UUID"];

TiLogMessage(@"[INFO] ===== %@ : discoverServices - for %@",self,peripheralUUID);

if (self.connectedPeripherals){
    for (CBPeripheral *peripheral in self.connectedPeripherals){
         if ([[peripheral.identifier UUIDString] isEqualToString:peripheralUUID]){
             TiLogMessage(@"[INFO] ===== %@ : discoverServices - , attempting to discover services for %@",self,peripheral);

             [peripheral discoverServices: [BLEServicesCBUUIDs count] > 0 ? BLEServicesCBUUIDs : nil];
         }
    }
}

TiLogMessage(@"[INFO] ===== %@ : discoverServices - exit",self);

}

【问题讨论】:

    标签: ios ios7 titanium bluetooth-lowenergy


    【解决方案1】:

    您需要强烈保留 CBPeripheral 实例(您正在使用的实例)。 例如,在您的视图控制器中,您需要有一个属性

    @property (strong, nonatomic) CBPeripheral *activePeripheral;

    将找到的外围设备分配给 activePeripheral,然后您的员工(连接/发现/等...)


    我认为使用像 CoreBluetooth 这样的框架并不是获得好的结果的好方法,你需要一些高级的、基于块的东西。这是我刚刚为您提交的一个库 https://github.com/LGBluetooth/LGBluetooth

    这将使使用蓝牙的生活更加轻松。

    【讨论】:

    • 谢谢。我终于让它工作了。我使用强大的 NSMutableArray 来保存发现的对象,这与使用您引用的属性相同,因为我正在发现多个对象。
    • 我已经在这样做了,但没有成功。我还是不明白为什么!!!请问有什么帮助/建议吗?
    • 问题是我在做自己的库,所以我不需要使用它
    【解决方案2】:

    我能够通过 CoreBluetooth 使用 NSMutableArray 来保存对象。这成功了:

    @property (strong, nonatomic) NSMutableArray *discoveredPeripherals;
    
    //then in your didDiscoverPeripheral delegate:
    if ( ![discoveredPeripherals containsObject peripheral]{
        [discoveredPeripherals addObject peripheral];       //this right here retains the obj
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-09
      • 2014-11-15
      • 1970-01-01
      • 2014-08-28
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多