单军华
2018-05-04 25f409185a53e5e7beb17518a684298d92d31b3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 
 
#import "DevicesTableViewCell.h"
#import "CBPeripheralExt.h"
#import "Constants.h"
 
/*!
 *  @class ScannedPeripheralTableViewCell
 *
 *  @discussion Model class for handling operations related to peripheral table cell
 *
 */
 
@implementation DevicesTableViewCell
{
    /*  Data fields  */
    __weak IBOutlet UILabel *RSSIValueLabel;
    __weak IBOutlet UILabel *peripheralAdressLabel;
    __weak IBOutlet UILabel *peripheralName;
}
- (void)awakeFromNib {
    // Initialization code
}
 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
 
    // Configure the view for the selected state
}
 
/*!
 *  @method nameForPeripheral:
 *
 *  @discussion Method to get the peripheral name
 *
 */
-(NSString *)nameForPeripheral:(CBPeripheralExt *)ble
{
    NSString *bleName ;
    
    if ([ble.mAdvertisementData valueForKey:CBAdvertisementDataLocalNameKey] != nil)
    {
        bleName = [ble.mAdvertisementData valueForKey:CBAdvertisementDataLocalNameKey];
    }
    
    if(bleName.length < 1 )
        bleName = ble.mPeripheral.name;
    
    if(bleName.length < 1 )
        bleName = UNKNOWN_PERIPHERAL;
    
    return bleName;
}
 
 
/*!
 *  @method UUIDStringfromPeripheral:
 *
 *  @discussion Method to get the UUID from the peripheral
 *
 */
-(NSString *)UUIDStringfromPeripheral:(CBPeripheralExt *)ble
{
    
    NSString *bleUUID = ble.mPeripheral.identifier.UUIDString;
    if(bleUUID.length < 1 )
        bleUUID = @"Nil";
    else
        bleUUID = [NSString stringWithFormat:@"UUID: %@",bleUUID];
    
    return bleUUID;
}
 
/*!
 *  @method ServiceCountfromPeripheral:
 *
 *  @discussion Method to get the number of services present in a device
 *
 */
-(NSString *)ServiceCountfromPeripheral:(CBPeripheralExt *)ble
{
    NSString *bleService =@"";
    NSInteger serViceCount = [[ble.mAdvertisementData valueForKey:CBAdvertisementDataServiceUUIDsKey] count];
    if(serViceCount < 1 )
        bleService = @"No Services";
    else
        bleService = [NSString stringWithFormat:@" %ld Service Advertised ",(long)serViceCount];
    
    return bleService;
}
 
#define RSSI_UNDEFINED_VALUE 127
 
 
/*!
 *  @method RSSIValue:
 *
 *  @discussion Method to get the RSSI value
 *
 */
-(NSString *)RSSIValue:(CBPeripheralExt *)ble
{
    
    NSString *deviceRSSI=[ble.mRSSI stringValue];
    
    if(deviceRSSI.length < 1 )
    {
        if([ble.mPeripheral respondsToSelector:@selector(RSSI)])
            deviceRSSI = ble.mPeripheral.RSSI.stringValue;
    }
    
    if([deviceRSSI intValue]>=RSSI_UNDEFINED_VALUE)
        deviceRSSI = @"Undefined";
    else
        deviceRSSI=[NSString stringWithFormat:@"%@ dBm",deviceRSSI];
    
    return deviceRSSI;
}
 
 
/*!
 *  @method setDiscoveredPeripheralDataFromPeripheral:
 *
 *  @discussion Method to display the device details
 *
 */
-(void)setDiscoveredPeripheralDataFromPeripheral:(CBPeripheralExt*) discoveredPeripheral
{
    peripheralName.text         = [self nameForPeripheral:discoveredPeripheral];
    peripheralAdressLabel.text  = [self ServiceCountfromPeripheral:discoveredPeripheral];
    RSSIValueLabel.text         = [self RSSIValue:discoveredPeripheral];
 
}
 
/*!
 *  @method updateRSSIWithValue:
 *
 *  @discussion Method to update the RSSI value of a device
 *
 */
-(void)updateRSSIWithValue:(NSString*) newRSSI
{
    RSSIValueLabel.text=newRSSI;
}
@end