Hi, I'm trying to make unity plugin for checking if Bluetooth is on or off on the iPhone/iPad. And I'm kinda stuck halfway. In my current plugin I just call one method that initializes CBCentralManager and then should check the state of the manager. But nothing happens when I call IsBluetoothEnabled in Unity. Like the delegate isn't being fired. But I do get the alert if I call it manually ([self centralManagerDidUpdateState:self.bluetoothManager] in detectBluetooth method. So calling the plugin and that works. Just not the CBCentralManager I guess.. Edit: The delegate is beeing called on bluetooth on/off cange, but the state is always unknown. Any ideas? P.S.: I'm no iOs developer, I snatched this code from stackoverflow. http://stackoverflow.com/questions/21676835/objective-c-check-if-bluetooth-availability-changed This is what I have: Code (CSharp): #import <CoreBluetooth/CoreBluetooth.h> @interface BluetoothPlugin : UIViewController <CBPeripheralDelegate, CBCentralManagerDelegate> { CBCentralManager *bluetoothManager; } @property (readwrite, nonatomic) CBCentralManager *bluetoothManager; @end @implementation BluetoothPlugin - (void)detectBluetooth { if(!self.bluetoothManager) { // Put on main queue so we can call UIAlertView from delegate callbacks. self.bluetoothManager = [[[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()] autorelease]; } } - (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSString *stateString = nil; switch(bluetoothManager.state) { case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break; case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break; case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break; case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break; case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break; default: stateString = @"State unknown, update imminent."; break; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth state" message:stateString delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil]; [alert show]; } @end static BluetoothPlugin* delegateObject = nil; extern "C" { bool _IsBluetoothEnabled() { if (delegateObject == nil) delegateObject = [BluetoothPlugin alloc]; [delegateObject detectBluetooth]; } }
Old post but in centralManagerDidUpdateState you need to call self.bluetoothManager instead of bluetoothManager, take out line 5 up there, and I had to remove autorelease because I was using ARC.