1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#
import
@interface
ViewController ()
@property
(nonatomic, retain) CBCentralManager *centralManager;
@end
@implementation
ViewController
- (
void
)viewDidLoad {
[
super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
|
1
|
|
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
|
/*
Invoked whenever the central manager's state is updated.
*/
- (
void
)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString * state = nil;
switch
([central state])
{
case
CBCentralManagerStateUnsupported:
state = @
"The platform/hardware doesn't support Bluetooth Low Energy."
;
break
;
case
CBCentralManagerStateUnauthorized:
state = @
"The app is not authorized to use Bluetooth Low Energy."
;
break
;
case
CBCentralManagerStatePoweredOff:
state = @
"Bluetooth is currently powered off."
;
break
;
case
CBCentralManagerStatePoweredOn:
state = @
"work"
;
break
;
case
CBCentralManagerStateUnknown:
default
:
;
}
NSLog(@
"Central manager state: %@"
, state);
}
|
1
2
3
|
- (IBAction)scan:(id)sender {
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
}
|
1
2
3
|
- (IBAction)scan:(id)sender {
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@
"FFE0"
]] options:nil];
}
|
1
2
3
4
5
6
7
|
- (
void
)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
static
int
i =
0
;
NSString *str = [NSString stringWithFormat:@
"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ "
, peripheral, RSSI, peripheral.UUID, advertisementData];
NSLog(@
"%@"
,str);
[self.discoverdPeriparals addObject:peripheral];
}
|
1
2
3
|
- (IBAction)connect:(id)sender {
[self.centralManager connectPeripheral:[self.discoverdPeriparals firstObject] options:nil];
}
|
1
|
|
1
2
3
4
5
6
7
8
9
10
11
|
/*
Invoked whenever a connection is succesfully created with the peripheral.
Discover available services on the peripheral
*/
- (
void
)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@
"Did connect to peripheral: %@"
, peripheral);
peripheral.delegate = self;
[central stopScan];
[peripheral discoverServices:nil];
}
|
1
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
- (
void
)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if
(error)
{
NSLog(@
"Discovered services for %@ with error: %@"
, peripheral.name, [error localizedDescription]);
return
;
}
for
(CBService *service in peripheral.services)
{
NSLog(@
"Service found with UUID: %@"
, service.UUID);
if
([service.UUID isEqual:[CBUUID UUIDWithString:@
"FFE0"
]])
{
[peripheral discoverCharacteristics:nil forService:service];
}
}
}
|
1
|
[peripheral discoverCharacteristics:nil forService:service];
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
- (
void
)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if
(error)
{
NSLog(@
"Discovered characteristics for %@ with error: %@"
, service.UUID, [error localizedDescription]);
return
;
}
for
(CBCharacteristic * characteristic in service.characteristics)
{
DLog(@
"%@"
,
if
( [characteristic.UUID isEqual:[CBUUID UUIDWithString:@
"FFE1"
]])
{
self.p = peripheral;
self.c = characteristic;
//read
//[testPeripheral readValueForCharacteristic:characteristic];
NSLog(@
"Found a Device Manufacturer Name Characteristic - Read manufacturer name"
);
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
- (
void
)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSString *RStr = @
"2764"
;
NSMutableString *str = [NSMutableString
new
];
[str appendFormat:@
"%c"
,
28
];
[str appendFormat:@
"%c"
,
33
];
[str appendFormat:@
"%c"
,
8
];
[self.p writeValue:[str dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)] forCharacteristic:self.c type:CBCharacteristicWriteWithResponse];
RStr = @
"吴水凤 你好呀!!!\n\n\n\n\n\n\n\n"
;
[self.p writeValue:[RStr dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)] forCharacteristic:self.c type:CBCharacteristicWriteWithResponse];
}
|
[peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];
该方法回调peripheral:didUpdateValueForCharacteristic:error:方法
1
|
|