Dhanunjaya
Dhanunjaya

Reputation: 500

multiple BLE devices which have same services and characteristics in ios

I have multiple BLE devices which have same services and characteristics. I can scan and connect multiple devices. After connections, when I try to distinguish each one by sending command it doesn't work. It works perfectly with a single device. Is it something like socket connection ? Like A server spawns child threads and each client can maintain a connection through threads.

Please provide some tips on how to scan each device when other device is reading the data from the device.

-(void) scanDevice {

  centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

  [centralManager scanForPeripheralsWithServices:nil options:0];

 [AppDelegate app].cbCentral = centralManager;

}

-(void) stopScan {

    [[AppDelegate app].cbCentral stopScan];
}

-(void)connectToDevice:(CBPeripheral *)peripheral{


  [[AppDelegate app] cbCentral].delegate = self;
  [[[AppDelegate app] cbCentral] connectPeripheral:peripheral options:nil];

}

 -(void)calldiscoverServicesForPeripheral:(CBPeripheral *)peripheral{

  [peripheral setDelegate:self];
  [peripheral discoverServices:nil];

}

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

NSLog(@"Connected PERIPHERAL %d",peripheral.state);

  [delegate getConnectedPeripheral:peripheral];

  NSLog(@"Connected peripheral %@",peripheral);
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {

   NSLog(@"Discovered servicea: %@", peripheral.services);

  for (CBService *service in peripheral.services) {
    NSLog(@"Discovered service: %@", [service.UUID data]);
    [peripheral discoverCharacteristics:nil forService:service];
  }
}

I will explain in detail,

I have table view, that contains BLE devices. For the first time it is empty, so i will search for the devices by calling a class "Scan Devices".

This "Scan devices" class contains all the Corebluetooth methods like CBCentralManager allocation, CBperipheral delegate methods.

After search, I will display the device in table view and connect to the BLE device. I am getting some data from "Scan Device" class.

Now, I want to search more devices to connect and get the data. For this, I will call [[CBCentralManager alloc] initWithDelegate:self] in "ScanDevices" class. At this time, for previous device(connected and reading) is showing the warning "is not a valid peripheral" and new device is connecting and reading data from the device.

But I want read the data from both devices at a time

Please help me... Thanks

Upvotes: 4

Views: 3002

Answers (1)

Paulw11
Paulw11

Reputation: 114875

You shouldn't keep creating a new CBCentral - doing so will cause your previous CBCentral to be deallocated and therefore invalidate the existing peripherals.

You should activate your scanning once, say in viewWillAppear and deactivate it in viewWillDisappear.

Once you have initiated scanning and set your delegate in your ScanDevices class, it will call [delegate getConnectedPeripheral:] each time a new peripheral is found and connected.

Upvotes: 1

Related Questions