Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "ContentView.h"
- @import CoreBluetooth;
- @interface BluetoothViewModel () <CBCentralManagerDelegate>
- @property (nonatomic, strong) CBCentralManager *centralManager;
- @property (nonatomic, strong) NSMutableArray<CBPeripheral *> *peripherals;
- @property (nonatomic, strong) CBPeripheral *selectedPeripheral;
- @property (nonatomic, strong) NSMutableArray<NSString *> *peripheralNames;
- @end
- @implementation BluetoothViewModel
- - (instancetype)init {
- self = [super init];
- if (self) {
- self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
- self.peripherals = [NSMutableArray new];
- self.peripheralNames = [NSMutableArray new];
- }
- return self;
- }
- - (NSArray<CBPeripheral *> *)getPeripherals {
- NSArray<CBPeripheral *> *filteredPeripherals = [self.peripherals filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(CBPeripheral *evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
- return evaluatedObject.name != nil;
- }]];
- return filteredPeripherals;
- }
- - (void)connectToPeripheral:(CBPeripheral *)peripheral {
- self.selectedPeripheral = peripheral;
- [self.centralManager connectPeripheral:peripheral options:nil];
- }
- - (void)disconnect {
- if (self.selectedPeripheral) {
- [self.centralManager cancelPeripheralConnection:self.selectedPeripheral];
- self.selectedPeripheral = nil;
- self.isConnected = NO;
- }
- }
- - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
- if (central.state == CBManagerStatePoweredOn) {
- [self.centralManager scanForPeripheralsWithServices:nil options:nil];
- }
- }
- - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
- if (![self.peripherals containsObject:peripheral]) {
- [self.peripherals addObject:peripheral];
- if (peripheral.name) {
- [self.peripheralNames addObject:peripheral.name];
- }
- }
- }
- - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
- self.isConnected = YES;
- [self.selectedPeripheral discoverServices:nil];
- }
- - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
- self.isConnected = NO;
- self.selectedPeripheral = nil;
- }
- @end
- @interface ContentView ()
- @property (nonatomic, strong) BluetoothViewModel *bluetoothViewModel;
- @end
- @implementation ContentView
- - (instancetype)init {
- self = [super init];
- if (self) {
- self.bluetoothViewModel = [[BluetoothViewModel alloc] init];
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self.bluetoothViewModel addObserver:self forKeyPath:@"isConnected" options:NSKeyValueObservingOptionNew context:nil];
- }
- - (void)dealloc {
- [self.bluetoothViewModel removeObserver:self forKeyPath:@"isConnected"];
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
- if ([keyPath isEqualToString:@"isConnected"]) {
- BOOL isConnected = [change[NSKeyValueChangeNewKey] boolValue];
- if (isConnected) {
- [self presentConnectedView];
- }
- }
- }
- - (void)presentConnectedView {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Connected Successfully" message:nil preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *disconnectAction = [UIAlertAction actionWithTitle:@"Disconnect"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement