IOS 蓝牙与外设交互

来源:互联网 发布:php 5.3.27.tar.gz 编辑:程序博客网 时间:2024/05/01 18:37

 1, 使用  

 CBCentralManager *   _manager = [[CBCentralManageralloc] initWithDelegate:selfqueue:nil];

 初始化一个中央设备(_manager);


判断手机是否开启了蓝牙只有central.state=CBCentralManagerStatePoweredOn  才能去链接蓝牙设备

//开始查看服务,蓝牙开启

-(void)centralManagerDidUpdateState:(CBCentralManager *)central

{

   switch (central.state) {

        caseCBCentralManagerStatePoweredOn:

            [selfupdateLog:@"蓝牙已打开,请扫描外设"];

           break;

       default:

           break;

    }

}



调用

 [_managerscanForPeripheralsWithServices:niloptions:@{CBCentralManagerScanOptionAllowDuplicatesKey :@YES }];

扫描外设蓝牙。


//查到外设后,停止扫描,连接设备

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

{

    [selfupdateLog:[NSStringstringWithFormat:@"已发现 peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData]];

   _peripheral = peripheral;

    NSLog(@"%@",_peripheral);

//停止扫描,在这里可根据peripheral.UUID去链接想要的蓝牙设备

    [self.managerstopScan];

    //[_activity stopAnimating];

   BOOL replace = NO;

    // Match if we have this device from before

   for (int i=0; i <_nDevices.count; i++) {

       CBPeripheral *p = [_nDevicesobjectAtIndex:i];

       if ([p isEqual:peripheral]) {

            [_nDevicesreplaceObjectAtIndex:i withObject:peripheral];

            replace =YES;

        }

    }

   if (!replace) {

        [_nDevicesaddObject:peripheral];

        [_deviceTablereloadData];

    }

}


_deviceTable可显示多个蓝牙设备。


使用

        [self.managerconnectPeripheral:_peripheraloptions:nil];

去链接蓝牙

断开蓝牙调用

[self.managercancelPeripheralConnection:_peripheral];


//连接外设成功,开始发现服务

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {

    [selfupdateLog:[NSStringstringWithFormat:@"成功连接 peripheral: %@ with UUID: %@",peripheral,peripheral.UUID]];

    [self.peripheralsetDelegate:self];

    [self.peripheraldiscoverServices:nil];

    [selfupdateLog:@"扫描服务"];

}


//连接外设失败

-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error

{

   NSLog(@"%@",error);

}


当rssi发生变化时

-(void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error

{

    //NSLog(@"%s,%@",__PRETTY_FUNCTION__,peripheral);

   int rssi = abs([peripheral.RSSIintValue]);

   CGFloat ci = (rssi - 49) / (10 * 4.);

   NSString *length = [NSStringstringWithFormat:@"发现BLT4.0热点:%@,距离:%.1fm",_peripheral,pow(10,ci)];

   NSLog(@"距离:%@",length);

}



//已发现服务

-(void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

    

    [selfupdateLog:@"发现服务."];

   int i=0;

   for (CBService *sin peripheral.services) {

        [self.nServicesaddObject:s];

    }

   for (CBService *sin peripheral.services) {

        [selfupdateLog:[NSStringstringWithFormat:@"%d :服务 UUID: %@(%@)",i,s.UUID.data,s.UUID]];

        i++;

        [peripheral discoverCharacteristics:nilforService:s];

    }

}


//已搜索到Characteristics

-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{

    [selfupdateLog:[NSStringstringWithFormat:@"发现特征的服务:%@ (%@)",service.UUID.data ,service.UUID]];

    

   for (CBCharacteristic *cin service.characteristics) {

        [selfupdateLog:[NSStringstringWithFormat:@"特征 UUID: %@ (%@)",c.UUID.data,c.UUID]];

        

       if ([c.UUIDisEqual:[CBUUIDUUIDWithString:@"2A06"]]) {

            _writeCharacteristic = c;

        }

       if ([c.UUIDisEqual:[CBUUIDUUIDWithString:@"2A19"]]) {

            [_peripheralreadValueForCharacteristic:c];

        }

        

       if ([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FFA1"]]) {

            [_peripheralreadRSSI];

        }

        [_nCharacteristicsaddObject:c];

    }

}



//获取外设发来的数据,不论是readnotify,获取数据都是从这个方法中读取。

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

{

    // BOOL isSaveSuccess;

    

   if ([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"2A19"]]) {

        NSString *value = [[NSStringalloc]initWithData:characteristic.valueencoding:NSUTF8StringEncoding];

       _batteryValue = [value floatValue];

        NSLog(@"电量%f",_batteryValue);

    }

   if ([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FFA1"]]) {

        NSString *value = [[NSStringalloc]initWithData:characteristic.valueencoding:NSUTF8StringEncoding];

        //_batteryValue = [value floatValue];

       NSLog(@"信号%@",value);

    }


   else

    NSLog(@"didUpdateValueForCharacteristic%@",[[NSStringalloc] initWithData:characteristic.valueencoding:NSUTF8StringEncoding]);

}







0 0
原创粉丝点击