1. 程式人生 > >藍芽開門智慧門鎖

藍芽開門智慧門鎖

本文主要是寫實現過程:
github 上完整Demo地址,大家可以下載看看:ZYiBeacon

專案需求:
業主反饋拿出門卡開門這一動作繁瑣,想要手機藍芽連線門口機進行搖一搖或者點選對應的大門,實現開門功能。

實現步驟:

1.  掃描藍芽裝置,將裝置列表展示
2.  連線其中一臺裝置
3.  連線成功後,向外設傳送一串開鎖指令
4.  傳送成功後,斷開和外設的連線

程式碼實現思路:
1. 需要藍芽管理者mgr 管理者可以掃描外圍裝置 遵守CBCentralManagerDelegate協議
2. mgr掃描到外設,與外設進行連線斷開連線資訊交流等一系列反饋回撥 需要遵循CBPeripheralDelegate協議

主要實現:
封裝一套管理類,在工程的任何地方 只要shareSYSearchPeriphalsMgr 就可以得到管理者

+ (SYSearchPeriphalsMgr *) shareSYSearchPeriphalsMgr{
    static SYSearchPeriphalsMgr * mgr;
    if (mgr == nil) {
        mgr = [[SYSearchPeriphalsMgr alloc]init];
        dispatch_queue_t centralQueue = dispatch_queue_create("no.nordicsemi.ios.nrftoolbox"
, DISPATCH_QUEUE_SERIAL); mgr.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:mgr queue:centralQueue]; mgr.peripherals = [NSMutableArray array]; mgr.orinPeripherals = [NSMutableArray array]; mgr.filter = NO; //初始化 mgr->UART_Service_UUID = [CBUUID UUIDWithString:uartServiceUUIDString]; mgr->UART_TX_Characteristic_UUID = [CBUUID UUIDWithString:uartTXCharacteristicUUIDString]; mgr->UART_RX_Characteristic_UUID = [CBUUID UUIDWithString:uartRXCharacteristicUUIDString]; [mgr starScan]; } return
mgr; }

是否掃描裝置:

/** 是否掃描裝置*/
- (int) scanForPeripherals:(BOOL)enable{
    if (self.bluetoothManager.state != CBManagerStatePoweredOn) {
        return -1;
    }

    dispatch_async(dispatch_get_main_queue(), ^{

        if (enable) {
            NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],CBCentralManagerScanOptionAllowDuplicatesKey, nil];
            if (self.filterUUID != nil) {
                [self.bluetoothManager scanForPeripheralsWithServices:@[self.filterUUID] options:options];
            }
            else{
                [self.bluetoothManager scanForPeripheralsWithServices:nil options:options];
            }

            self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
        }
        else{
            [self.timer invalidate];
            self.timer = nil;

            [self.bluetoothManager stopScan];
        }

    });
    return 0;
}

連線裝置:

[self.bluetoothManager cancelPeripheralConnection:self.bluetoothPeripheral];

// 通過UART服務檢視是否是自己的藍芽裝置 是的話建立連線後 則可以向裝置傳送資訊 不然裝置會接收不到
// 裝置接收到資訊後,經過處理,驗證成功後則開鎖
/** 裝置特徵值*/
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error{
    NSLog(@"向裝置傳送開鎖指令 === %@",peripheral.name);
    if (error)
    {
        NSLog(@"Characteristics discovery failed");
        NSLog(@"error : %@",error);

    }
    else
    {
        NSLog(@"Characteristics discovered");

        if ([service.UUID isEqual:UART_Service_UUID]) {
            CBCharacteristic *txCharacteristic = nil;


            for (CBCharacteristic *characteristic in service.characteristics)
            {
                NSLog(@"Characteristics ==== %@ ",characteristic);
                if ([characteristic.UUID isEqual:UART_TX_Characteristic_UUID])
                {
                    NSLog(@"TX Characteristic found");
                    txCharacteristic = characteristic;
                }
                else if ([characteristic.UUID isEqual:UART_RX_Characteristic_UUID])
                {
                    NSLog(@"RX Characteristic found");
                    self.uartRXCharacteristic = characteristic;


                    // 3. 向裝置傳送開鎖指令
                    // 傳送資料
                    [self send:@"1234567" withByteCount:7];
                    NSString *command = self.sendText;
                    [self send:command withByteCount:20];

                    NSLog(@"向裝置傳送開鎖指令 === %@",command);

                    // 4. 失去裝置連線
                    [self disconnectDevice];

                }
            }
        }
    }

}