0

I am not new to iOS coding but very new to BLE in iOS.

I have successfully written code in my Main View Controller to access and write to a BLE device. So all the peripheral and manager stuff is tested.

My app will get a list of BLE devices, connect to them for the serial number, disconnect from them, and then put them in a tableView. When the user selects one, I need to go to a different view and reconnect. I want to write another class as a utility to connect to the peripheral, which I am successfully passing to the utility and read and write to it in the utility. The peripheral.state is connected.

Every time I try to write to the peripheral, my code crashes. However the peripheral reports that the data was written.

So to figure out what was wrong I started to just try and read the peripheral with the same results, Crash.

I'm starting a new manager in the utility and centralManagerDidUpdateState is not getting called there. I also tried it without the manager thinking that if I knew of the peripheral, it would just work.

So, how do I get centralManager to work in the new class? Do you have to somehow stop the manager in the main class?

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

device_Info_UUID = [CBUUID UUIDWithString:kDevice_Info_SID];
security_UUID = [CBUUID UUIDWithString:kSecurity_SID];
status_UUID = [CBUUID UUIDWithString:kStatus_SID];
device_ID_char_UUID =[CBUUID UUIDWithString:kSecurity_char_DeviceID];

SHA256in = [[NSMutableString alloc]initWithString:lockMasterKey];

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

peripheral.delegate = self;
[self.mgr connectPeripheral:peripheral options:nil];

This is the line works in the main controller but not in my Utility.

 [self.lock writeValue:myID forCharacteristic:charac type:CBCharacteristicWriteWithResponse];

Thanks!

Scott
  • 60
  • 10
  • Update: I tried the Utility in a new View Controller and centralManager works just fine. So the issue is with the manager not starting in an NSObject class. Does that help? – Scott Mar 15 '16 at 19:11
  • What is the crash? I am not sure you can "transfer" a CBPeripheral instance acquired from one CBCentralManager to another. Either use a single CBCentralManager instance throughout your app, perhaps in a Bluetooth manager class, or use `retrievePeripheralWithIdentifier` on your second CBcentralManager. – Paulw11 Mar 15 '16 at 19:26
  • The crash is the dreaded Red Bad Thread with no reference in Main.m. I did try the retrievePeripheralWithIdentifier but the delegate did not get called. I think the main issue is that when I pass a peripheral to a View Controller CBPeripheralManager works fine. It's when I try to pass it to a plain NSObject. Do you have a reference to your suggestion of using a single instance of the manager? BTW, thanks for responding – Scott Mar 15 '16 at 19:38
  • There is no specific reason that a viewcontroller subclass would work while a nsobject subclass won't unless there are other issues; for example the nsobject instance being released while it is still the active delegate while a view controller Simon screen so it doesn't get released. You can use a Bluetooth manager object which is either a singleton or held by your AppDelegate or even just stored on a property of each VC and set on the next VC in `prepareForSegue`. Even doing this with a single CBCentralManager would be better than having multiple. – Paulw11 Mar 15 '16 at 19:49
  • Thanks Paul. I was also thinking that the nsobject wouldn't matter other than it is a delegate of the VC that calls it. I agree with the efficiency of having a single manager, it's just that I've not done that yet. Do you have a reference? I;m going to search for that now. Thanks again!! – Scott Mar 15 '16 at 20:25

1 Answers1

-1

I am not sure passing a peripheral to other viewcontroller is a "good idea" unless you don't need further update on the peripheral. If you want a "fresh" peripheral in multiple view controller i recommend to use singleton design thus you can handle all delegates at once and reduce redundant codes.

sample code I found. https://github.com/kickingvegas/YmsCoreBluetooth