我正在尝试将一个字符写入我的蓝牙LE设备,到目前为止它已经有效,但只有当我在didDiscoverCharacteristicsForService方法中执行时才这样做。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) {
[self cleanup];
return;
}
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual: CHAR_UUID]) {
NSLog(@"Discovered characteristic: %@", characteristic);
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
_discoveredCharacteristic = characteristic;
char c;
count++;
if (count%2 == 1)
{
c = 0x0;
}
else
{
c = 0x7;
}
NSData *data = [NSData dataWithBytes: &c length: 1];
[_discoveredPeripheral writeValue:data forCharacteristic:_discoveredCharacteristic
type:CBCharacteristicWriteWithResponse];
NSLog(@"Writing value %@ for characteristic %@", data, _discoveredCharacteristic.UUID);
}
}
timer = [NSTimer scheduledTimerWithTimeInterval:2
target:self selector:@selector(targetMethod:)
userInfo:nil repeats:YES];
_repeatingTimer = timer;
}
但是当我将writeValue部分移动到由计时器触发的方法时, 它不再将值写入我的设备。为什么是这样?我做错了什么?
- (void)targetMethod:(NSTimer*)theTimer{
char c;
count++;
if (count%2 == 1)
{
c = 0x0;
}
else
{
c = 0x7;
}
NSData *data = [NSData dataWithBytes: &c length: 1];
[_discoveredPeripheral writeValue:data forCharacteristic:_discoveredCharacteristic
type:CBCharacteristicWriteWithResponse];
NSLog(@"Writing value %@ for characteristic %@", data, _discoveredCharacteristic.UUID);
}
我对iOS和蓝牙都很陌生。
答案 0 :(得分:0)
我愚蠢,写完后我正在关闭与外围设备的连接。