blePeripheral 蓝牙透传

来源:互联网 发布:mysql怎么用 编辑:程序博客网 时间:2024/05/30 23:16

//http://www.szrfstar.com/loadlist.aspx?type=7&classid=12&act=3

//  blePeripheral.m

//  MonitoringCenter

//

//  Created by David ding on 13-1-10.

//

//


#import "blePeripheral.h"

#import "bleTransmitMoudelAppDelegate.h"


//================== TransmitMoudel =====================

// TransmitMoudel Receive Data Service UUID

NSString *kReceiveDataServiceUUID                       =@"FFE0";

// TransmitMoudel characteristics UUID

NSString *kReceive05BytesDataCharateristicUUID          =@"FFE1";

NSString *kReceive10BytesDataCharateristicUUID          =@"FFE2";

NSString *kReceive15BytesDataCharateristicUUID          =@"FFE3";

NSString *kReceive20BytesDataCharateristicUUID          =@"FFE4";


// TransmitMoudel Send Data Service UUID

NSString *kSendDataServiceUUID                          =@"FFE5";

// TransmitMoudel characteristics UUID

NSString *kSend05BytesDataCharateristicUUID             =@"FFE6";

NSString *kSend10BytesDataCharateristicUUID             =@"FFE7";

NSString *kSend15BytesDataCharateristicUUID             =@"FFE8";

NSString *kSend20BytesDataCharateristicUUID             =@"FFE9";


@implementation blePeripheral{

   NSTimer         *autoSendDataTimer;

   UInt16          testSendCount;

}



#pragma mark -

#pragma mark Init

/******************************************************/

//          类初始化                                   //

/******************************************************/

// 初始化蓝牙

-(id)init{

   self = [superinit];

   if (self) {

        [selfinitPeripheralWithSeviceAndCharacteristic];

        [selfinitPropert];

    }

    return self;

}


-(void)setActivePeripheral:(CBPeripheral *)AP{

    _activePeripheral = AP;

    // 把外设的名字(CBPERIPHECAL 的对象)取出来

   NSString *aname = [[NSStringalloc]initWithFormat:@"%@",_activePeripheral.name];

   NSLog(@"aname:%@",aname);

   if (![anameisEqualToString:@"(null)"]) {

        //如果名字不为空,再把它赋值给 blePeriphecal对象的名字。

       _nameString = aname;

    }

   else{

        _nameString =@"Error Name";

    }

   NSString *auuid = [[NSStringalloc]initWithFormat:@"%@",_activePeripheral.UUID];

   if (auuid.length >=36) {

        _uuidString = [auuidsubstringWithRange:NSMakeRange(auuid.length-36,36)];

        NSLog(@"uuidString:%@",_uuidString);

    }

}



-(void)initPeripheralWithSeviceAndCharacteristic{

    // CBPeripheral

    [_activePeripheralsetDelegate:nil];

    _activePeripheral =nil;

    // CBService and CBCharacteristic

    _ReceiveDataService =nil;

    _Receive05BytesDataCharateristic =nil;

    _Receive10BytesDataCharateristic =nil;

    _Receive15BytesDataCharateristic =nil;

    _Receive20BytesDataCharateristic =nil;

    _SendDataService =nil;

    _Send05BytesDataCharateristic =nil;

    _Send10BytesDataCharateristic =nil;

    _Send15BytesDataCharateristic =nil;

    _Send20BytesDataCharateristic =nil;

}


-(void)initPropert{

    // Property

    _staticString =@"Init";

    _currentPeripheralState =blePeripheralDelegateStateInit;

    nPeripheralStateChange

    _connectedFinish =kDisconnected;

    _receiveData =0;

    _sendData = 0;

    _txCounter = 0;

    _rxCounter = 0;

    _ShowStringBuffer = [[NSStringalloc]init];

    _AutoSendData =NO;

    

    [autoSendDataTimerinvalidate];

    autoSendDataTimer =nil;

    testSendCount =0;

}


#pragma mark -

#pragma mark Scanning

/****************************************************************************/

/*   Scanning                                    */

/****************************************************************************/

// UUID进行扫描

-(void)startPeripheral:(CBPeripheral *)peripheral DiscoverServices:(NSArray *)services{

   if ([peripheralisEqual:_activePeripheral] && [peripheralisConnected]){

       _activePeripheral = peripheral;

        [_activePeripheralsetDelegate:(id<CBPeripheralDelegate>)self];

        [_activePeripheraldiscoverServices:services];

    }

}


-(void)disconnectPeripheral:(CBPeripheral *)peripheral{

   if ([peripheralisEqual:_activePeripheral]){

       //内存释放

        [selfinitPeripheralWithSeviceAndCharacteristic];

        [selfinitPropert];

    }

}


#pragma mark -

#pragma mark CBPeripheral

/****************************************************************************/

/*                              CBPeripheral */

/****************************************************************************/

// 扫描服务

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

   if (!error)

    {

       if ([peripheralisEqual:_activePeripheral]){

           //新建服务数组

           NSArray *services = [peripheralservices];

           if (!services || ![servicescount])

            {

               NSLog(@"发现错误的服务 %@\r\n", peripheral.services);

            }

           else

            {

               //开始扫描服务

               _staticString =@"Discover services";

                _currentPeripheralState =blePeripheralDelegateStateDiscoverServices;

                nPeripheralStateChange

               for (CBService *servicesin peripheral.services)

                {

                   NSLog(@"发现服务UUID: %@\r\n", services.UUID);

                    //================== TransmitMoudel =====================// FFE0

                   if ([[servicesUUID]isEqual:[CBUUIDUUIDWithString:kReceiveDataServiceUUID]])

                    {  //如果该服务的uuid FFE0表示这个服务是用来接收数据的。再把该服务赋给私有变量。

                        

                       //扫描接收数据服务特征值

                       _ReceiveDataService = services;

                        [peripheraldiscoverCharacteristics:nilforService:_ReceiveDataService];

                    }

                    //================== TransmitMoudel =====================// FFE5

                   elseif ([[servicesUUID]isEqual:[CBUUIDUUIDWithString:kSendDataServiceUUID]])

                    {

                       //扫描发送数据服务特征值

                       _SendDataService = services;

                        [peripheraldiscoverCharacteristics:nilforService:_SendDataService];

                    }

                    //======================== END =========================

                }

            }

        }

    }

}


// 从服务中扫描特征值

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

   if (!error) {

       if ([peripheralisEqual:_activePeripheral]){

           //开始扫描特征值

            _staticString =@"Discover characteristics";

            //前面一个是发现服务,这一个是发现特征。。

            _currentPeripheralState =blePeripheralDelegateStateDiscoverCharacteristics;

            nPeripheralStateChange

           //新建特征值数组

           NSArray *characteristics = [servicecharacteristics];

           CBCharacteristic *characteristic;

            //================== TransmitMoudel =====================// FFE1 FFE2 FFE3 FFE4

           if ([[serviceUUID]isEqual:[CBUUIDUUIDWithString:kReceiveDataServiceUUID]])

            { //先根据服务的uuid来判断这个服务是发送还是接收服务。再通过for循环判断每个这个服务中的每个特征是什么特征。

               for (characteristicin characteristics)

                {

                   NSLog(@"发现特值UUID: %@\n", [characteristic UUID]);

                   if ([[characteristicUUID] isEqual:[CBUUIDUUIDWithString:kReceive05BytesDataCharateristicUUID]])

                    {

                       _Receive05BytesDataCharateristic = characteristic;

                        // 在这个外设中, 打开 05bytes 这个特征值 的通知功能。

                        [peripheralsetNotifyValue:YESforCharacteristic:characteristic];

                    }

                   elseif ([[characteristicUUID]isEqual:[CBUUIDUUIDWithString:kReceive10BytesDataCharateristicUUID]])

                    {

                       _Receive10BytesDataCharateristic = characteristic;

                        [peripheralsetNotifyValue:YESforCharacteristic:characteristic];

                    }

                   elseif ([[characteristicUUID]isEqual:[CBUUIDUUIDWithString:kReceive15BytesDataCharateristicUUID]])

                    {

                       _Receive15BytesDataCharateristic = characteristic;

                        [peripheralsetNotifyValue:YESforCharacteristic:characteristic];

                    }

                   elseif ([[characteristicUUID]isEqual:[CBUUIDUUIDWithString:kReceive20BytesDataCharateristicUUID]])

                    {

                       _Receive20BytesDataCharateristic = characteristic;

                        [peripheralsetNotifyValue:YESforCharacteristic:characteristic];

                    }

                }

            }

            //================== TransmitMoudel =====================// FFE6 FFE7 FFE8 FFE9

           elseif ([[serviceUUID]isEqual:[CBUUIDUUIDWithString:kSendDataServiceUUID]])

            {

               for (characteristicin characteristics)

                {

                   NSLog(@"发现特值UUID: %@\n", [characteristic UUID]);

                   if ([[characteristicUUID] isEqual:[CBUUIDUUIDWithString:kSend05BytesDataCharateristicUUID]])

                    {

                       _Send05BytesDataCharateristic = characteristic;

                    }

                   elseif ([[characteristicUUID]isEqual:[CBUUIDUUIDWithString:kSend10BytesDataCharateristicUUID]])

                    {

                       _Send10BytesDataCharateristic = characteristic;

                    }

                   elseif ([[characteristicUUID]isEqual:[CBUUIDUUIDWithString:kSend15BytesDataCharateristicUUID]])

                    {

                       _Send15BytesDataCharateristic = characteristic;

                    }

                   elseif ([[characteristicUUID]isEqual:[CBUUIDUUIDWithString:kSend20BytesDataCharateristicUUID]])

                    {

                       _Send20BytesDataCharateristic = characteristic;

                        

                       //完成连接

                        [selfFinishConnected];

                        //如果是发送特征,没有调用  [peripheral setNotifyValue:YES forCharacteristic:characteristic]; 这个函数,到了 20BYTES这个特征的时候, 调用   [self FinishConnected];表示连接完成。。

                    }

                }

            }

            //======================== END =========================

        }

    }

}


// 更新特征值  第三个是更新特征。。。

// 先判断有没有 报错, 再判断传进来的外设跟私有变量中的外设是否一致,是的话,再判断传进来的这个特征值是否为四种接收类型中的一种如果是,把这个特征的值赋值给NSDATA REVICEDATE (私有变量)再调用[self receiveData:_receiveData]接收数据的函数。

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

   if ([errorcode] ==0) {

       if ([peripheralisEqual:_activePeripheral]){

            //===================== AntiLost ========================FE21FE22FE23FE24FE25

           if ([characteristicisEqual:_Receive05BytesDataCharateristic])

            {

                //if (characteristic.value.length == TRANSMIT_05BYTES_DATA_LENGHT) {

                   _receiveData = characteristic.value;

                    [selfreceiveData:_receiveData];

               //}

            }

           //接收温湿度数据

           elseif ([characteristicisEqual:_Receive10BytesDataCharateristic])

            {

                //if (characteristic.value.length == TRANSMIT_10BYTES_DATA_LENGHT) {

                   _receiveData = characteristic.value;

                    [selfreceiveData:_receiveData];

               //}

            }

           //读取名字

           elseif ([characteristicisEqual:_Receive15BytesDataCharateristic])

            {

                //if (characteristic.value.length == TRANSMIT_15BYTES_DATA_LENGHT) {

                   _receiveData = characteristic.value;

                    [selfreceiveData:_receiveData];

               //}

            }

            

           elseif ([characteristicisEqual:_Receive20BytesDataCharateristic])

            {

                //if (characteristic.value.length == TRANSMIT_20BYTES_DATA_LENGHT) {

                   _receiveData = characteristic.value;

                    [selfreceiveData:_receiveData];

               //}

            }

            //======================== END =========================

        }

    }

   else{

       NSLog(@"参数更新出错: %d",[error code]);

    }

}


#pragma mark -

#pragma mark read/write/notification

/******************************************************/

//          读写通知等基础函数                           //

/******************************************************/

// 写数据到特征值

-(void) writeValue:(CBPeripheral *)peripheral characteristic:(CBCharacteristic *)characteristic data:(NSData *)data{

   if ([peripheralisEqual:_activePeripheral] && [peripheralisConnected])

    {

       if (characteristic !=nil) {

           NSLog(@"成功写数据到特征值: %@数据:%@\n", characteristic.UUID, data);

            [peripheral writeValue:dataforCharacteristic:characteristictype:CBCharacteristicWriteWithoutResponse];

        }

    }

}


// 从特征值读取数据

-(void) readValue:(CBPeripheral *)peripheral characteristicUUID:(CBCharacteristic *)characteristic{

   if ([peripheralisEqual:_activePeripheral] && [peripheralisConnected])

    {

       if (characteristic !=nil) {

           NSLog(@"成功从特征值:%@读数据\n", characteristic);

            [peripheralreadValueForCharacteristic:characteristic];

        }

    }

}


// 发通知到特征值

-(void) notification:(CBPeripheral *)peripheral characteristicUUID:(CBCharacteristic *)characteristic state:(BOOL)state{

   if ([peripheralisEqual:_activePeripheral] && [peripheralisConnected])

    {

       if (characteristic !=nil) {

           NSLog(@"成功发通知到特征值: %@\n", characteristic);

            [peripheralsetNotifyValue:stateforCharacteristic:characteristic];

            //- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error 跟这个函数中的方法一样,如果接收特征,则设为YES,不过这里的STATE 根据传进来的值来赋值。 通知这个外设的某个特征的状态是还是

        }

    }

}


#pragma mark -

#pragma mark Set property

/******************************************************/

//              BLE属性操作函数                          //

/******************************************************/

-(void)FinishConnected{

    // 更新标志

    _connectedFinish =YES;

    _staticString =@"Connected finish";

    _currentPeripheralState =blePeripheralDelegateStateKeepActive;

    nPeripheralStateChange

    NSLog(@"连接完成\n");

}


-(void)receiveData:(NSData *)data{

   Byte dataLength = data.length;

    // 接收计数加1

    _rxCounter++;

    

   Byte data2Byte[dataLength];

    //把传进来的数据放到这个字节类型的变量中。再把这个字节类型变量中的数据转成字符串。再把这个字符串添加到显示缓存中

    [datagetBytes:&data2Bytelength:dataLength];

    NSString *dataASCII = [[NSStringalloc]initWithBytes:data2Bytelength:dataLengthencoding:NSASCIIStringEncoding];

    [selfaddReceiveASCIIStringToShowStringBuffer:dataASCII];

   _staticString = [[NSStringalloc]initWithFormat:@"Receive:%@",dataASCII];

    nUpdataShowStringBuffer

}


-(void)addReceiveASCIIStringToShowStringBuffer:(NSString *)aString{

    //在接到的数据前面叠加"PC:"后面加入换行后添加到显示缓存

   NSString *rxASCII = [[NSStringalloc]initWithFormat:@"        PC:"];

    rxASCII = [rxASCIIstringByAppendingString:aString];

    rxASCII = [rxASCIIstringByAppendingString:@"\n"];

    _ShowStringBuffer = [_ShowStringBufferstringByAppendingString:rxASCII];

}


-(void)setSendData:(NSData *)data{

   Byte dataLength = data.length;

    

    if ((dataLength ==TRANSMIT_05BYTES_DATA_LENGHT) || (dataLength ==TRANSMIT_10BYTES_DATA_LENGHT) || (dataLength ==TRANSMIT_15BYTES_DATA_LENGHT) || (dataLength ==TRANSMIT_20BYTES_DATA_LENGHT)) {

        // 发送计数加1

       _txCounter++;

        

       Byte data2Byte[dataLength];

        [datagetBytes:&data2Bytelength:dataLength];

        NSString *dataASCII = [[NSStringalloc]initWithBytes:data2Bytelength:dataLengthencoding:NSASCIIStringEncoding];

        if (dataLength ==TRANSMIT_05BYTES_DATA_LENGHT)

        { //把数据写到特征值中

            [selfwriteValue:_activePeripheralcharacteristic:_Send05BytesDataCharateristicdata:data];

        }

        elseif (dataLength ==TRANSMIT_10BYTES_DATA_LENGHT)

        {

            [selfwriteValue:_activePeripheralcharacteristic:_Send10BytesDataCharateristicdata:data];

        }

        elseif (dataLength ==TRANSMIT_15BYTES_DATA_LENGHT)

        {

            [selfwriteValue:_activePeripheralcharacteristic:_Send15BytesDataCharateristicdata:data];

        }

        elseif (dataLength ==TRANSMIT_20BYTES_DATA_LENGHT)

        {

            [selfwriteValue:_activePeripheralcharacteristic:_Send20BytesDataCharateristicdata:data];

        }

        [selfaddSendASCIIStringToShowStringBuffer:dataASCII];

       _staticString = [[NSStringalloc]initWithFormat:@"Send:%@",dataASCII];

        nUpdataShowStringBuffer

    }

}


-(void)addSendASCIIStringToShowStringBuffer:(NSString *)aString{

    //在发送的数据前面叠加"IP:"后面加入换行后添加到显示缓存

   NSString *txASCII = [[NSStringalloc]initWithFormat:@"IP:"];

    txASCII = [txASCIIstringByAppendingString:aString];

    txASCII = [txASCIIstringByAppendingString:@"\n"];

    _ShowStringBuffer = [_ShowStringBufferstringByAppendingString:txASCII];

}


-(void)setAutoSendData:(BOOL)AutoSendData{

   if (AutoSendData ==YES) {

        // 自动发送测试数据s

       if (autoSendDataTimer !=nil) {

            [autoSendDataTimerinvalidate];

        }

        autoSendDataTimer = [NSTimerscheduledTimerWithTimeInterval:kAutoSendTestDataTimertarget:selfselector:@selector(AutoSendDataEvent)userInfo:nilrepeats:YES];

    }

   else{

        [autoSendDataTimerinvalidate];

        autoSendDataTimer =nil;

        testSendCount =0;

    }

}


-(void)AutoSendDataEvent{

    // 发送数据自动加1

    if (_activePeripheral.isConnected ==YES) {

        testSendCount++;

       NSString *txAccString = [[NSStringalloc]initWithFormat:@"%05d",testSendCount];

       NSString *test20ByteASCII = [[NSStringalloc]initWithFormat:@"ABCDEFGHIJKLMNO"];

        test20ByteASCII = [test20ByteASCIIstringByAppendingString:txAccString];

        [selfsetSendData:[test20ByteASCIIdataUsingEncoding:NSASCIIStringEncoding]];

    }

   else{

        [autoSendDataTimerinvalidate];

        autoSendDataTimer =nil;

        testSendCount =0;

        

        _connectedFinish =kDisconnected;

        _AutoSendData =NO;

        

        _staticString =@"Disconnect";

        _currentPeripheralState =blePeripheralDelegateStateInit;

        nPeripheralStateChange

    }

}


@end


原创粉丝点击