Advertisement
SnoopSheep2001

Core-bluetooth in obj-C

Mar 24th, 2023
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Objective C 3.66 KB | Source Code | 0 0
  1. #import "ContentView.h"
  2. @import CoreBluetooth;
  3.  
  4. @interface BluetoothViewModel () <CBCentralManagerDelegate>
  5.  
  6. @property (nonatomic, strong) CBCentralManager *centralManager;
  7. @property (nonatomic, strong) NSMutableArray<CBPeripheral *> *peripherals;
  8. @property (nonatomic, strong) CBPeripheral *selectedPeripheral;
  9. @property (nonatomic, strong) NSMutableArray<NSString *> *peripheralNames;
  10.  
  11. @end
  12.  
  13. @implementation BluetoothViewModel
  14.  
  15. - (instancetype)init {
  16.     self = [super init];
  17.     if (self) {
  18.         self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
  19.         self.peripherals = [NSMutableArray new];
  20.         self.peripheralNames = [NSMutableArray new];
  21.     }
  22.     return self;
  23. }
  24.  
  25. - (NSArray<CBPeripheral *> *)getPeripherals {
  26.     NSArray<CBPeripheral *> *filteredPeripherals = [self.peripherals filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(CBPeripheral *evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  27.         return evaluatedObject.name != nil;
  28.     }]];
  29.     return filteredPeripherals;
  30. }
  31.  
  32. - (void)connectToPeripheral:(CBPeripheral *)peripheral {
  33.     self.selectedPeripheral = peripheral;
  34.     [self.centralManager connectPeripheral:peripheral options:nil];
  35. }
  36.  
  37. - (void)disconnect {
  38.     if (self.selectedPeripheral) {
  39.         [self.centralManager cancelPeripheralConnection:self.selectedPeripheral];
  40.         self.selectedPeripheral = nil;
  41.         self.isConnected = NO;
  42.     }
  43. }
  44.  
  45. - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
  46.     if (central.state == CBManagerStatePoweredOn) {
  47.         [self.centralManager scanForPeripheralsWithServices:nil options:nil];
  48.     }
  49. }
  50.  
  51. - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
  52.     if (![self.peripherals containsObject:peripheral]) {
  53.         [self.peripherals addObject:peripheral];
  54.         if (peripheral.name) {
  55.             [self.peripheralNames addObject:peripheral.name];
  56.         }
  57.     }
  58. }
  59.  
  60. - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
  61.     self.isConnected = YES;
  62.     [self.selectedPeripheral discoverServices:nil];
  63. }
  64.  
  65. - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
  66.     self.isConnected = NO;
  67.     self.selectedPeripheral = nil;
  68. }
  69.  
  70. @end
  71.  
  72. @interface ContentView ()
  73.  
  74. @property (nonatomic, strong) BluetoothViewModel *bluetoothViewModel;
  75.  
  76. @end
  77.  
  78. @implementation ContentView
  79.  
  80. - (instancetype)init {
  81.     self = [super init];
  82.     if (self) {
  83.         self.bluetoothViewModel = [[BluetoothViewModel alloc] init];
  84.     }
  85.     return self;
  86. }
  87.  
  88. - (void)viewDidLoad {
  89.     [super viewDidLoad];
  90.     [self.bluetoothViewModel addObserver:self forKeyPath:@"isConnected" options:NSKeyValueObservingOptionNew context:nil];
  91. }
  92.  
  93. - (void)dealloc {
  94.     [self.bluetoothViewModel removeObserver:self forKeyPath:@"isConnected"];
  95. }
  96.  
  97. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  98.     if ([keyPath isEqualToString:@"isConnected"]) {
  99.         BOOL isConnected = [change[NSKeyValueChangeNewKey] boolValue];
  100.         if (isConnected) {
  101.             [self presentConnectedView];
  102.         }
  103.     }
  104. }
  105.  
  106. - (void)presentConnectedView {
  107.     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Connected Successfully" message:nil preferredStyle:UIAlertControllerStyleAlert];
  108.     UIAlertAction *disconnectAction = [UIAlertAction actionWithTitle:@"Disconnect"
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement