Search Unity

Bluetooth plugin for untiy

Discussion in 'iOS and tvOS' started by markoslo, Nov 27, 2014.

  1. markoslo

    markoslo

    Joined:
    Oct 18, 2013
    Posts:
    1
    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):
    1. #import <CoreBluetooth/CoreBluetooth.h>
    2.  
    3. @interface BluetoothPlugin : UIViewController <CBPeripheralDelegate, CBCentralManagerDelegate>
    4. {
    5.     CBCentralManager *bluetoothManager;
    6. }
    7. @property (readwrite, nonatomic) CBCentralManager *bluetoothManager;
    8. @end
    9.  
    10.  
    11. @implementation BluetoothPlugin
    12.  
    13. - (void)detectBluetooth
    14. {
    15.     if(!self.bluetoothManager)
    16.     {
    17.         // Put on main queue so we can call UIAlertView from delegate callbacks.
    18.         self.bluetoothManager = [[[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()] autorelease];
    19.     }
    20. }
    21.  
    22. - (void)centralManagerDidUpdateState:(CBCentralManager *)central
    23. {
    24.     NSString *stateString = nil;
    25.     switch(bluetoothManager.state)
    26.     {
    27.         case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
    28.         case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
    29.         case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
    30.         case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
    31.         case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
    32.         default: stateString = @"State unknown, update imminent."; break;
    33.     }
    34.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth state" message:stateString delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    35.     [alert show];
    36. }
    37.  
    38. @end
    39.  
    40. static BluetoothPlugin* delegateObject = nil;
    41.  
    42. extern "C"
    43. {
    44.  
    45.     bool _IsBluetoothEnabled()
    46.     {
    47.         if (delegateObject == nil)
    48.             delegateObject = [BluetoothPlugin alloc];
    49.         [delegateObject detectBluetooth];
    50.  
    51.     }
    52.  
    53. }
     
    Last edited: Nov 27, 2014
  2. Cherubim79

    Cherubim79

    Joined:
    May 3, 2014
    Posts:
    56
    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.