简体   繁体   English

调用discoverServices之后,iOS BLE外设立即保持断开连接

[英]iOS BLE peripherals keep disconnecting immediately after discoverServices is called

While developing a BLE app for iOS, I keep getting a disconnect immediately after discoverServices is called. 在开发适用于iOS的BLE应用程序时,在调用discoverServices之后,我会立即断开连接。 I am testing with 4 exact BLE devices (OEM), and I keep getting this disconnect on exactly the same two devices. 我正在使用4个完全一样的BLE设备(OEM)进行测试,并且在完全相同的两个设备上不断断开连接。 Every time. 每次。 I know the devices are ok, because I've also built the same app on Android, and with the same devices, all 4 stay connected. 我知道设备还可以,因为我还在Android上构建了相同的应用程序,并且使用相同的设备,所有4个设备始终保持连接。 This is using Titanium, but everything here is implemented in iOS. 这使用的是Titanium,但是这里的所有内容都在iOS中实现。 Here's the relevant iOS code: 以下是相关的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);

} }

You need to keep CBPeripheral instance (by which you are working) strongly. 您需要严格保持CBPeripheral实例(您正在使用的实例)。 For example in your view controller you need to have a property 例如,在您的视图控制器中,您需要具有一个属性

@property (strong, nonatomic) CBPeripheral *activePeripheral; @属性(强,非原子)CBPeripheral * activePeripheral;

Assign found peripheral to activePeripheral, after which do your staff (connecting/discovering/etc...) 将找到的外围设备分配给activePeripheral,之后再进行工作人员(连接/发现/等...)


I think using Framework like a CoreBluetooth is not a good way to achieve good results, you need something high level, block based. 我认为像CoreBluetooth这样使用Framework并不是取得良好结果的好方法,您需要基于块的高级功能。 Here is a library that I have just commited for you https://github.com/LGBluetooth/LGBluetooth 这是我刚刚为您提交的一个库https://github.com/LGBluetooth/LGBluetooth

It will make life with bluetooth much more easier. 它将使蓝牙的生活更加轻松。

I was able to get this working with CoreBluetooth, using an NSMutableArray to hold the objects. 我可以使用NSMutableArray来保存对象,并与CoreBluetooth一起使用。 This did the trick: 这达到了目的:

@property (strong, nonatomic) NSMutableArray *discoveredPeripherals;

//then in your didDiscoverPeripheral delegate:
if ( ![discoveredPeripherals containsObject peripheral]{
    [discoveredPeripherals addObject peripheral];       //this right here retains the obj
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM