last day (18 days later) » 

11:09 AM
0
Q: Write data to multiple BLE(bluetooth) devices

user3774439Please check the following scenario: ConnectDevices Class: This method is for connect to BLE device. I am calling this method from ViewController to connect to the BLE device. -(void)connectToDevice:(CBPeripheral *)peripheral{ [[AppDelegate app] cbCentral].delegate = self; [[[AppD...

 
I am guessing it is because you are passing different peripherals but always using the same self.uartCharacteristic value but it is hard to tell because there is a lot of poorly formatted code here. It would be a lot cleaner if you just stored a reference to your singleton instead of calling `[[AppDelegate app] connectDevices]’ everywhere. It would probably be a good idea to wrap your CBPeripheral in another object so you could abstract a lot of the methods and the characteristic property
 
Thanks Paulw11. Please check the updated question. How can I create self.uartcharacteristic for every device in ConnectDevice class. Please dont hesitate. I am new to this mutilthreading. I didn't understand the "CBPeripheral in another object". Could you please explain little more. Please help me.
 
What I meant was to create another object which has a CBperipheral as a property and can also store your characteristics etc associated with that peripheral.
 
Thanks alot.What I am understanding, create an object CBPeripheral in ConnectDevice class and assign the new CBPeripheral to this local object in Write Method. Is that Right?
 
I would create it as soon as you first learn about the peripheral - either in didDiscoverPeripheral or didRetrievePeripheral depending on how you are discovering the peripheral
 
11:09 AM
Thanks for reply. As I already creat an object of CBCharacteristics and every time I am assigning new value to that. Is this right? Could you please help me. I am struggling with this issue for 2 days. please help me
Hi
 
You only have one property for your characteristic and you keep overwriting each time you connect to a new peripheral. By creating a new object to wrap your peripheral you can store the cbcharacteristic with it. You can also move methods that are associated with a peripheral into that class.
 
One more request...I have implemented multithreading. Is that correct way? I mean NSOperation subclass
 
You probably don't need it. Receive events will call your delegate methods and once you call the write method on a characteristic CoreBluetooth automatically completes the write asynchronously
 
but two ble devices I need to connect at a time for that we need it right?
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{

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

CBPeripheral *new_peripheral = peripheral;

for (CBService *service in new_peripheral.services) {

if ([service.UUID isEqual: [Utility SERVICE_UUID]]){
[new_peripheral discoverCharacteristics:@[[Utility UART_UUID]] forService:service];
}
}
}
Is this way I a creating CBperipheral object
 
11:32 AM
No, Core Bluetooth IO is non-blocking, so you don't need additional threads
You should create an object something like this -
MyPeripheral.h

@interface MyPeripheral : NSObject <CBPeripheralDelegate>



@property (strong, nonatomic) CBPeripheral *peripheral;
@property (weak,nonatomic) id delegate;


@end

-(id) initWithPeripheral:(CBPeripheral *)periphral;

MyPeripheral.m


@interface MyPeripheral()

@property (nonatomic,strong) CBCharacteristic *uartCharacteristic

@end

@implementation MyPeripheral

-(id) initWithPeripheral:(CBPeripheral *)peripheral {

if (self=[super init]) {
In your didConnectPeripheral you would write

MyPeripheral *p=[[MyPeripheral alloc]initWithPeripheral:peripheral];
p.delegate = (Whatever your delegate for read call backs is. Self? )
[self.connectedPeripherals addObject:p]; // This is a property that is an NSMutableArray to store all of your connected peripherals
 
11:51 AM
thank u so much i will try n let u know..once again thanks alot paul
 
you're welcome
 

  last day (18 days later) »