私はiPhoneアプリに取り組んでおり、iOS 7.0および8.0でCoreBluetoothフレームワークを使用して
Immediate Alert serviceをサポートするBluetooth Low Energyデバイスに値を書き込む必要があります。デバイスとの接続は完全に機能していますが、デバイスに値を書き込もうとしても何も起こりません(値はデバイスに保存されないため、デバイスは呼び出し音を鳴らしません)。
以下は、値を書き込むために使用されるコードです。
+ (void)writeValueForCharacteristic:(CBCharacteristic *)characteristic peripheral:(CBPeripheral *)peripheral alarm:(BOOL)alarm
{
NSData * data = nil;
if (alarm) {
uint8_t value = 2;
data = [NSData dataWithBytes:&value length:sizeof(value)];
} else {
uint8_t value = 0;
data = [NSData dataWithBytes:&value length:sizeof(value)];
}
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
}
上記のコードを実行する前のデバイスのログとステータス:
- (void)beginAlarm
{
NSLog(@"characteristic : %@",discAlertCharacteristic);
NSLog(@"peripheral : %@",discPeripheral);
NSLog(@"service : %@",service);
[BluetoothUtility writeValueForCharacteristic:discAlertCharacteristic peripheral:discPeripheral alarm:YES];
}
特性:CBCharacteristic:0x146ea8d0、UUID = 2A06、プロパティ= 0x14、値=(null)、通知= NO
サービス:CBService:0x146ecc90、isPrimary = YES、UUID = 1802
周辺機器:CBPeripheral:0x146dd110、識別子= B0A195DC-7273-B3F5-BA70-0219A61F8904、名前= LA-TAG a87、状態=接続済み
BLE Utility appを使用してBluetoothデバイスをテストし、そのデバイスが正しく動作することを確認しました。
if (getCharacteristic.properties.rawValue == 0xE){
let sentValue:[UInt8] = [0x38, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x02]
let sentData = Data(bytes: sentValue)
self.peripheralDevice.writeValue(sentData, for: getCharacteristic, type: CBCharacteristicWriteType.withResponse)
}