蓝牙LE无法从中心向外围设备发送数据

时间:2015-07-30 13:56:45

标签: ios bluetooth bluetooth-lowenergy

我正在处理BluetoothLow Energy问题。我发现我的外围BLE设备与iPhone连接,也找到服务和处于写入模式的特性。但是当我尝试发送一些数据时,没有任何反应 - 设备没有收到任何内容,也没有调用- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

我的writeValue方法在这里:

-(void) writeValue:(int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p data:(NSData *)data {

    CBUUID *UUIDService = [CBUUID UUIDWithString:[NSString stringWithFormat:@"%@",TRANSFER_SERVICE_UUID]];
    CBUUID *UUIDCharacteristic = [CBUUID UUIDWithString:[NSString stringWithFormat:@"%@",TRANSFER_CHARACTERISTIC_UUID]];

    NSLog(@"ALERT");
    CBService *service = [self findServiceFromUUID:UUIDService p:p];
    if (!service)
        {
            NSLog(@"Could not find service with UUID %@ on peripheral with UUID %@",
                  [self CBUUIDToString:UUIDService],
                  p.identifier.UUIDString);

            return;
        }

    CBCharacteristic *characteristic = [self findCharacteristicFromUUID:UUIDCharacteristic service:service];
    if (!characteristic)
    {
        NSLog(@"Could not find characteristic with UUID %@ on service with UUID %@ on peripheral with UUID %@",
              [self CBUUIDToString: UUIDCharacteristic],
              [self CBUUIDToString:UUIDService],
              p.identifier.UUIDString);

        return;
    }

    NSString *message = @"AB";
    NSData *dataMsg= [message dataUsingEncoding:NSUTF8StringEncoding];

    [p writeValue:dataMsg forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

}

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

试试这个。迅捷3 此代码用于启用CBCharacteristic的通知(0x12表示通知属性)。

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

    for newChar: CBCharacteristic in service.characteristics!{

        peripheral.readValue(for: newChar)

        if newChar.properties.rawValue == 0x12{

            peripheral.setNotifyValue(true, for: newChar)
        }
    }
}