By exchanging information directly between non-adjacent protocol layers, cross-layer (CL) interaction can significantly improve and optimize network performances such as energy efficiency and delay. This is particularly important for wireless sensor networks (WSNs) where sensor devices are energy-constrained and deployed for real-time monitoring applications. Existing CL schemes mainly exploit information exchange between physical, medium access control (MAC), and routing layers, with only a handful involving application layer. For the first time, we proposed a framework for CL optimization based on user context of ambient intelligence (AmI) application and an ontology-based context modeling and reasoning mechanism. We applied the proposed framework to jointly optimize MAC and network (NET) layer protocols for WSNs. Extensive evaluations show that the resulting optimization through context awareness and CL interaction for both MAC and NET layer protocols can yield substantial improvements in terms of throughput, packet delivery, delay, and energy performances.