蓝牙协议

1 首先我带入了BLE包,实现蓝牙协议


2 扫描周围的蓝牙设备

 self.beaconFinder = [KRBeaconFinder sharedFinder];

    

    __weak typeof(self) weakSelf = self;

    [_beaconFinder setBleScanningEnumerator:^(CBPeripheral *peripheral, NSDictionary *advertisements, NSNumber *RSSI)

     {

//         NSLog(@"The advertisement with identifer: %@, state: %d, name: %@, services: %@,  description: %@",

//               [peripheral identifier],

//               [peripheral state],

//               [peripheral name],

//               [peripheral services],

//               [advertisements description]);

         

         

         //过滤(hasPrefix

         if ([peripheral.name hasPrefix:@"XY"]) {

             

             if (![weakSelf.detectedBeacons containsObject:peripheral]) {

                 [weakSelf.detectedBeacons addObject:peripheral];

                 [weakSelf.beaconTableview reloadData];

             }


         }

         

     }];

//启动

    [_beaconFinder bleScan];


3 连接蓝牙设备


    [[KRBeaconCentralManager sharedManager].centralManager connectPeripheral:beacon options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];


3.1 连接设备成功后会回调一个方法


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

{

    //...

      

}


外设设置代理以便以后数据交流

peripheral.delegate = self;


查询蓝牙服务

[peripheral discoverServices:nil];

5.1蓝牙服务通知通过的回调

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

{

    

}

查询服务所带的特征值

[peripheral discoverCharacteristics:nil forService:service];


6.1返回的蓝牙特征值通知通过代理实现的回调方法

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

{   

}

7 给蓝牙发送数据

[peripheral readValueForCharacteristic:service.characteristics[0]]



7.1 给蓝牙发送数据成功的代理回调方法

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

{

}

8 处理蓝牙发过来得数据的回调方法

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

{


    [ peripheral writeValue:tempData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];    

}




个人敲了一下测试过的,希望有大神帮忙指点一下。。。。。。。。。。。。。。。。













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值